Skip to content Skip to sidebar Skip to footer

How To Open A Webview From Imagebutton?

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

Solution 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().

Solution 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(newView.OnClickListener() {

        @OverridepublicvoidonClick(View v) {


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


        }});

Solution 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"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/uhwallpaper"><ImageButtonandroid: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.
*/publicvoidmyMethod(View view){
    Intent browserIntent = newIntent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/universityofhouston"));
    startActivity(browserIntent);
}

Does it help?

Solution 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);

Post a Comment for "How To Open A Webview From Imagebutton?"