当我在操作栏上设置开关按钮但是引发了一些错误。
04-10 12:17:36.986 5121-5121/in.spotrack.soubhagya E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.spotrack.soubhagya, PID: 5121
java.lang.NullPointerException: Attempt to invoke interface method 'android.view.View android.view.MenuItem.getActionView()' on a null object reference
at in.spotrack.soubhagya.Operator.onCreateOptionsMenu(Operator.java:28)
at android.app.Activity.onCreatePanelMenu(Activity.java:3373)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:328)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332)
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1370)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1650)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这是我们可以用开关按钮给菜单充气的代码。
package in.spotrack.soubhagya;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
public class Operator extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_operator);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem itemSwitch = menu.findItem(R.id.actionMenu);
Log.i("Abhinav", String.valueOf(R.layout.switch_layout));
itemSwitch.setActionView(R.layout.switch_layout);
final Switch sw=menu.findItem(R.id.actionMenu).getActionView().findViewById(R.id.switchView);
getMenuInflater().inflate(R.menu.mainmenu,menu);
return super.onCreateOptionsMenu(menu);
}
}
menu.xml代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/actionMenu"
android:title=""
app:showAsAction="always"
android:actionLayout="@layout/switch_layout"/>
</menu>
switch_layout code。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Switch
android:id="@+id/switchView"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_weight="1"
android:scaleX="1.3"
android:scaleY="1.3"
android:switchMinWidth="45dp"
android:padding="6dp"
/>
</LinearLayout>
答案 0 :(得分:0)
在onCreateOptionsMenu()方法中,getActionView()给出null,因此应用程序输出NullPOinterException。
final Switch sw=menu.findItem(R.id.actionMenu).getActionView().findViewById(R.id.switchView);
了解NullPointerException,你去here。
哟还可以打印
System.err.print(menu.findItem(R.id.actionMenu));
并查看它的内容,并检查实体类是否正确发送数据。还应检查Getter和Setter。