How to open a webview from imagebutton?

核能气质少年 提交于 2019-12-13 17:53:22

问题


I have three buttons in a fragment that when the user clicks on one it'll open a specific webpage within the application. When I try to run this code on the phone it crashes when I try to access the tab.

My .java class is right here:

public class SocialMediaFragment extends Fragment {

public ImageButton fbbutton;
public ImageButton twbutton;
public ImageButton igbutton;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

  View rootView = inflater.inflate(R.layout.fragment_socialmedia, container, false);

    fbbutton = (ImageButton) getActivity().findViewById(R.id.fbbutton);
    twbutton = (ImageButton) getActivity().findViewById(R.id.twbutton);
    igbutton = (ImageButton) getActivity().findViewById(R.id.igbutton);

    fbbutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/universityofhouston"));
            startActivity(browserIntent);
        }
    });

    twbutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("website"));
            startActivity(browserIntent);

        }});

    igbutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("website"));
            startActivity(browserIntent);

        }});
    return rootView;

}
}

And here is the fragment (XML) file I am using:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/uhwallpaper">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fbbutton"
        android:background="@drawable/fb"
        android:layout_marginStart="46dp"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true" />


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/twbutton"
        android:background="@drawable/twitter"
        android:layout_above="@+id/igbutton"
        android:layout_toEndOf="@+id/fbbutton"
        android:layout_marginStart="43dp" />



    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/igbutton"
        android:background="@drawable/instagram"
        android:layout_below="@+id/fbbutton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp" />



    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/socialslogan"
        android:background="@drawable/slogan_social"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="16dp" />

</RelativeLayout>

I hope this is fixable! If you need any more information let me know.

Here is my error report:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at info.teammumu.cougarcardapp.SocialMediaFragment.onCreateView(SocialMediaFragment.java:28)

回答1:


Your fbbutton is never assigned. It is null as it is by default. Initialize it just like other views from your layout via findViewById().




回答2:


1) You are inside a fragment, so your elements must be defined inside the layout fragment_socialmedia.xml

    View rootView = inflater.inflate(R.layout.fragment_socialmedia, container, false);

   mDrawerLayout = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) rootView.findViewById(R.id.list_slidermenu);
    fbbutton = (ImageButton) rootView.findViewById(R.id.fbbutton);
    twbutton = (ImageButton) rootView.findViewById(R.id.twbutton);
    igbutton = (ImageButton) rootView.findViewById(R.id.igbutton);

2), add @override to the onClick method:

 fbbutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/universityofhouston"));
            startActivity(browserIntent);


        }});



回答3:


First of all I believe that it should work the way Elenasys is supposing it. Another try would be to change your layout xml and attach the method to call within the xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/uhwallpaper">

<ImageButton
    android:onClick="myMethod"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fbbutton"
    android:background="@drawable/fb"
    android:layout_marginStart="46dp"
    android:layout_centerVertical="true"
    android:layout_alignParentStart="true" />

...

</RelativeLayout>

Then you have to write a method inside your activity holding your fragment like this:

/**
* Method inside the activity holding your fragment.
*/
public void myMethod(View view){
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/universityofhouston"));
    startActivity(browserIntent);
}

Does it help?




回答4:


Turns out the problem was with these guys:

fbbutton = (ImageButton) getActivity().findViewById(R.id.fbbutton);
twbutton = (ImageButton) getActivity().findViewById(R.id.twbutton);
igbutton = (ImageButton) getActivity().findViewById(R.id.igbutton);

they're supposed to be:

fbbutton = (ImageButton) rootView.findViewById(R.id.fbbutton);
twbutton = (ImageButton) rootView.findViewById(R.id.twbutton);
igbutton = (ImageButton) rootView.findViewById(R.id.igbutton);


来源:https://stackoverflow.com/questions/29014167/how-to-open-a-webview-from-imagebutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!