启动活动时,我不断收到nullpointerexception。我试图获取井字游戏的分数,以显示在TextView的操作栏上,但是每当我尝试在onPrepareOptionsMenu()方法中设置文本时,应用程序就会崩溃。实际上,我正在使用ActionView将小部件放置在操作栏中。

这是我的res / menu / score_show.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/scoreTextItem"
        yourapp:showAsAction="always|withText"
        android:actionViewClass="android.widget.TextView"
        yourapp:actionViewClass="@layout/score_text"/>

</menu>


还有我的res / layout / score_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scoreText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >


</TextView>


最后,是TicTacToeActivity.java的一个片段:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.score_show, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    scoreItem = menu.findItem(R.id.scoreTextItem);
    TextView scoreText = (TextView)MenuItemCompat.getActionView(scoreItem);
    scoreText.setText("P1: " + humanScore + " P2: " + computerScore);
    supportInvalidateOptionsMenu();
    return super.onPrepareOptionsMenu(menu);
}


这是我的日志

03-13 00:40:09.244: D/AndroidRuntime(1127): Shutting down VM
03-13 00:40:09.248: E/AndroidRuntime(1127): FATAL EXCEPTION: main
03-13 00:40:09.248: E/AndroidRuntime(1127): Process: com.packagename.tictactoe, PID: 1127
03-13 00:40:09.248: E/AndroidRuntime(1127): java.lang.NullPointerException
03-13 00:40:09.248: E/AndroidRuntime(1127):     at com.packagename.tictactoe.TicTacToeActivity.onPrepareOptionsMenu(TicTacToeActivity.java:618)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.app.Activity.onPreparePanel(Activity.java:2556)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.support.v4.app.FragmentActivity.onPrepareOptionsPanel(FragmentActivity.java:491)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.support.v7.app.ActionBarActivity.superOnPrepareOptionsPanel(ActionBarActivity.java:240)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.support.v7.app.ActionBarActivityDelegate.onPrepareOptionsPanel(ActionBarActivityDelegate.java:160)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.support.v7.app.ActionBarActivity.onPrepareOptionsPanel(ActionBarActivity.java:212)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:480)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.support.v7.app.ActionBarActivity.superOnPreparePanel(ActionBarActivity.java:236)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.support.v7.app.ActionBarActivityDelegateICS.onPreparePanel(ActionBarActivityDelegateICS.java:155)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.support.v7.app.ActionBarActivity.onPreparePanel(ActionBarActivity.java:204)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onPreparePanel(ActionBarActivityDelegateICS.java:290)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:464)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.view.Choreographer.doFrame(Choreographer.java:543)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.os.Handler.handleCallback(Handler.java:733)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.os.Looper.loop(Looper.java:136)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at java.lang.reflect.Method.invokeNative(Native Method)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at java.lang.reflect.Method.invoke(Method.java:515)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-13 00:40:09.248: E/AndroidRuntime(1127):     at dalvik.system.NativeStart.main(Native Method)


谢谢!

最佳答案

除非您设置了自定义MenuItem#getActionView(),否则null将返回ActionView

您应该做的是使用scoreItem.setTitle()

10-08 03:40