无法处理操作栏上的切换按钮

时间:2016-07-21 09:57:45

标签: android android-actionbar

我已在this question中阅读@Fabricio PH回复,以便在ActionBar上制作切换按钮。

然后我尝试了他的第二种方式。它显示在Action Bar上,但我无法在其上设置任何属性。

这是我的代码:

main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:radastaff="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/gps_toggle"
        android:title="GPS Toggle"
        radastaff:showAsAction="always"
        radastaff:actionLayout="@layout/actionbar_layout_toggle"
        />
</menu>

此菜单项的布局:

action_bar_layout_toggle.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ToggleButton
        android:id="@+id/actionbar_gps_toggle"
        android:layout_width="71dp"
        android:layout_height="30dp"
        android:textOn=""
        android:textOff=""
        android:background="@drawable/gps_status_png"
        android:layout_marginRight="20dp"/>
</RelativeLayout>

在MainActivity.java中 - &gt; onCreate(),我这样调用了一个ToggleButton,但是它说findviewbyId()是null对象。

ToggleButton toggle = (ToggleButton)findViewById(R.id.actionbar_gps_toggle);
toggle.setChecked(true);

logcat的:

07-21 17:03:25.926 8914-8914/com.rada.radastaff E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.rada.radastaff, PID: 8914
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rada.radastaff/com.rada.radastaff.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ToggleButton.setChecked(boolean)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
        at android.app.ActivityThread.access$800(ActivityThread.java:147)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5253)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ToggleButton.setChecked(boolean)' on a null object reference
        at com.rada.radastaff.MainActivity.onCreate(MainActivity.java:20)
        at android.app.Activity.performCreate(Activity.java:5975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376) 
        at android.app.ActivityThread.access$800(ActivityThread.java:147) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5253) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372)

提前致谢。

1 个答案:

答案 0 :(得分:1)

在您的情况下,ToggleButton位于菜单中,因此您应该在onCreateOptionsMenu而非onCreate

内收到
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.menu_search, menu);

    MenuItem item = menu.findItem(R.id.gps_toggle);
    // get the root layout of your menu item, in your case it is RelativeLayout
    RelativeLayout rootView = (RelativeLayout)item.getActionView();
    // get the ToggleButton in RelativeLayout 
    ToggleButton = (ToggleButton) rootView.findItem(R.id.actionbar_gps_toggle);

    return true;
}