FacebookSdk尝试在空对象引用上调用虚拟方法'android.view.View android.view.View.findViewById(int)'

时间:2019-03-06 18:36:49

标签: java android facebook share

最近我想使用Facebook api在我的应用程序中添加Facebook共享按钮,但是在尝试在我的设备上安装该应用程序时出现此错误:

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)

这是我的onCreate方法:

    @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);
            }
        }
    });
}

这是我的布局(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>

我认为这个抽屉有问题。也许当我创建按钮时,findviewbyid出现了问题,但我想我已经尝试了所有方法,但没有任何效果。

1 个答案:

答案 0 :(得分:1)

您正在activity_main_drawer.xml中使用菜单项。您确定您的类文件中具有重写的onCreateOptionsMenu()函数吗?因为您需要给菜单充气。

here中,您可以看到所需的方法。