我知道这个问题已经存在,但我尝试了几个解决方案,没有一个对我有效。
就我而言,我需要在ActionBar中使用Switch
按钮。
item
的id
编辑:代码中的异常点标有注释。它是主要的活动。 :)
以下是我的一些代码:
mainactivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_minepedia_main);
switchButton = (Switch) findViewById(R.id.switch_button);
//The following line throws an exception, because switchButton
//is null, even though I tried to take it on the upper line.
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//The following is a local boolean variable
if (isChecked) {
ONLINE_MODE_ENABLED = true;
} else {
ONLINE_MODE_ENABLED = false;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; --> main_actions.xml
getMenuInflater().inflate(R.menu.main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
mainactivity_layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".Minepedia_main">
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_centerInParent="true"
android:gravity="center" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/items_button"
android:onClick="startItemsMenu" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/about_button"
android:onClick="startAboutScreen" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:paddingTop="50dp"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/update_offline_button"
android:onClick="startOfflineUpdate"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/offline_sync_info"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
main_actions.xml(菜单):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item
android:id="@+id/myswitch"
android:title=""
android:showAsAction="always"
android:actionLayout="@layout/switch_layout"
/>
</menu>
switch_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:id="@+id/switch_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text=""
android:textOff="Offline"
android:textOn="Online"/>
</RelativeLayout>
堆栈跟踪:
08-20 17:45:52.902 5534-5534/com.nubage.minepedia E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.nubage.minepedia, PID: 5534
java.lang.NullPointerException
at com.nubage.minepedia.Minepedia_main.onCreateOptionsMenu(Minepedia_main.java:63)
at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:489)
at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:853)
at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:273)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:543)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5081)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
由于Switch
按钮位于main_actions.xml
内,因此您应使用menu.findItem
中的onCreateOptionsMenu
来访问菜单中的视图:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_actions, menu);
MenuItem item = menu.findItem(R.id.switch_button);
switchButton = (Switch)item.getActionView();
return super.onCreateOptionsMenu(menu);
}
答案 1 :(得分:1)
按照'ρяσѕρєяK'的回答,但改变他的以下代码行:
MenuItem item = menu.findItem(R.id.switch_button);
到此:
MenuItem item = menu.findItem(R.id.myswitch); //the id was changed!
那应该摆脱NullPointerException,希望这有助于:)
所以你应该有这样的东西:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_minepedia_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_actions, menu);
MenuItem item = menu.findItem(R.id.myswitch);
switchButton = (Switch)item.getActionView();
switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener({
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//The following is a local boolean variable
if (isChecked) {
ONLINE_MODE_ENABLED = true;
} else {
ONLINE_MODE_ENABLED = false;
}
}
});
return super.onCreateOptionsMenu(menu);
}
另外更改此行:
android:actionLayout="@layout/switch_layout"
到此:
android:actionViewClass="android.widget.Switch"
这是在您的菜单xml中。如果您应用该更改,那么您正在使用android提供的切换按钮,并且必须正常工作。