操作栏菜单项中的空指针异常

时间:2017-03-28 06:46:31

标签: android nullpointerexception

我在操作栏中实现了切换它给了我错误尝试调用虚方法' void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton $ OnCheckedChangeListener)'在空对象引用上

menu xml

<item
    android:id="@+id/toggleservice"
    android:actionViewClass="android.widget.Switch"
    android:showAsAction="ifRoom"
    android:title=""/>

主要活动

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    final MenuItem toggleservice = menu.findItem(R.id.toggleservice);
    final Switch actionView = (Switch) toggleservice.getActionView();
    actionView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(MainActivity.this, "UnChecked", Toast.LENGTH_SHORT).show();
            }

        }
    });
    return super.onCreateOptionsMenu(menu);

}

logcat错误

03-28 12:09:43.556 10445-10445/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.example.prem.weatherapp, PID: 10445
                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
                                                   at com.example.prem.weatherapp.activity.MainActivity.onCreateOptionsMenu(MainActivity.java:92)
                                                   at android.app.Activity.onCreatePanelMenu(Activity.java:2850)
                                                   at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:362)
                                                   at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
                                                   at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335)
                                                   at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
                                                   at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
                                                   at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
                                                   at android.os.Handler.handleCallback(Handler.java:746)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:148)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

提前致谢

0 个答案:

没有答案