Android 开关(按钮)引发NullPointerException
我知道这个问题已经存在,但我尝试了几种解决方案,没有一种对我有效 在我的例子中,我需要操作栏中的Android 开关(按钮)引发NullPointerException,android,android-actionbar,android-button,Android,Android Actionbar,Android Button,我知道这个问题已经存在,但我尝试了几种解决方案,没有一种对我有效 在我的例子中,我需要操作栏中的开关按钮 我试图手动添加minSdk版本 我试图在定义的xml中使用项的id 编辑:代码中的异常点用注释标记。它在主活动中。:) 以下是我的一些代码: main活动: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
开关按钮
- 我试图手动添加minSdk版本
- 我试图在定义的xml中使用
项的id
编辑:代码中的异常点用注释标记。它在主活动中。:)
以下是我的一些代码:
main活动:
@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);
}
<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>
<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>
<?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)
main活动\u布局:
@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);
}
<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>
<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>
<?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)
由于Switch
按钮位于main\u actions.xml
内,因此您应该使用oncreateoptions菜单中的menu.findItem
访问菜单中的视图,如下所示:
@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);
}
在答案后面加上“ρцσѕρєц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提供的切换按钮,该按钮必须工作。哪一行引发异常?将日志添加到question@vokilam:Main活动代码中对此有注释。:)但是我应该给你一个通知。谢谢你的回答。我试过了,但仍然得到一个NullPointerException@`switchButton=(Switch)项;我也尝试了这个方法,但我无法通过此更改摆脱空指针:(@TrudleR你需要自己的开关按钮布局吗?我最近在我的操作栏中放置了一个开关,没有任何问题,但是我没有使用自己的布局,我只是使用android提供的标准开关(开/关)。@TrudleR你可以发布/更新你的MainActivity(java)代码吗?你能把这行:android:actionLayout=“@layout/switch\u layout”改为:android:actionViewClass=“android.widget.switch”吗/>这在您的菜单xml中。只是想看看它是否与您的自定义布局有关,因为如果您应用了更改,那么您使用的是android提供的切换按钮,这必须起作用。@TrudleR我在最后的答案中添加了注释(我想这是您的意思)。没问题,我很高兴它能起作用:)