Zuzanna
Zuzanna

Reputation: 1

FacebookSdk Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

Recently I wanted to add Facebook share button in my app with the Facebook api, but I get this error when trying to install the app on my device:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uz.zgora.tourdebar/com.uz.zgora.tourdebar.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2723)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2784)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6238)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.uz.zgora.tourdebar.MainActivity.onCreate(MainActivity.java:95)
    at android.app.Activity.performCreate(Activity.java:6857)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2676)

This is my onCreate method:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FacebookSdk.sdkInitialize(this.getApplicationContext());

    buttonShare = (Button) findViewById(R.id.nav_share);

    callbackManager = CallbackManager.Factory.create();
    shareDialog = new ShareDialog(this);

    buttonShare.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ShareLinkContent linkContent = new ShareLinkContent.Builder().setQuote("This is useful link").setContentUrl(Uri.parse("https://google.de")).build();
            if(ShareDialog.canShow(ShareLinkContent.class)){
                shareDialog.show(linkContent);
            }
        }
    });
}

and this my layout (activity_main_drawer.xml):

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_map"
        android:icon="@drawable/ic_menu_google_map"
        android:title="Map" />
    <item
        android:id="@+id/nav_list"
        android:icon="@drawable/ic_menu_bar_list"
        android:title="List" />
</group>

<item android:title="Communicate">
    <menu>
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_menu_share"
            android:title="Share" />
        <item
            android:id="@+id/nav_send"
            android:icon="@drawable/ic_menu_send"
            android:title="Send" />
    </menu>
</item>

I think that there is a problem with this drawer. Maybe when I'm creating the button there is problem with findviewbyid, but I think I've tried everything and nothing works.

Upvotes: 0

Views: 82

Answers (1)

IceMan
IceMan

Reputation: 123

From your activity_main_drawer.xml, you are using menu items. Are you sure you have the overriden onCreateOptionsMenu() function in your class file? Because you need to inflate the menu.

From here, you can see the required way for this.

Upvotes: 1

Related Questions