结束游戏时的Android游戏崩溃(NullPointerException)

时间:2015-06-20 17:37:17

标签: android debugging nullpointerexception

我正在设计一款有效的游戏,直到我在主要活动中添加了保存分数的功能。一旦我做了,分数保存工作,但在每个最后阶段,游戏崩溃与以下错误代码:

    java.lang.NullPointerException
            at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280)
            at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129)
            at android.os.Binder.execTransact(Binder.java:407)
            at dalvik.system.NativeStart.run(Native Method)
06-20 10:18:09.961     944-1573/? I/InputMethodManagerService﹕ IME STATUS OFF
06-20 10:18:09.961     944-1573/? W/InputMethodManagerService﹕ Got RemoteException sending setActive(false) notification to pid 15857 uid 10202
06-20 10:18:09.971  20141-20718/? W/chromium﹕ [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
06-20 10:18:09.981  20682-20682/? W/dalvikvm﹕ method Landroid/support/v7/internal/widget/ListViewCompat;.lookForSelectablePosition incorrectly overrides package-private method with same name in Landroid/widget/ListView;
06-20 10:18:09.981  20141-20210/? I/Ads﹕ CsiReporterFactory: CSI is not enabled. No CSI reporter created.
06-20 10:18:09.981  20141-20724/? I/Ads﹕ CsiReporterFactory: CSI is not enabled. No CSI reporter created.
06-20 10:18:09.991  20141-20724/? W/BaseRuntimeLoader﹕ don't searchcom.lge.telephony.msim.QcomMSimTelephonyManagerAdaptor class
06-20 10:18:09.991  20141-20724/? W/BaseRuntimeLoader﹕ don't searchcom.lge.telephony.msim.MtkMSimTelephonyManagerAdaptor class
06-20 10:18:09.991  20141-20724/? W/BaseRuntimeLoader﹕ don't searchcom.lge.telephony.msim.QcomMSimTelephonyManagerAdaptor class
06-20 10:18:09.991  20141-20724/? W/BaseRuntimeLoader﹕ don't searchcom.lge.telephony.msim.MtkMSimTelephonyManagerAdaptor class
06-20 10:18:10.021  20495-20495/? V/RenderScript﹕ Application requested CPU execution

这就是我保存的结构:在游戏结束时(由游戏类中的boolean gameOver表示),我的处理程序会收到通知,然后从游戏类中提取得分,然后使用saveScore方法保存SharedPreferences。

我不确定为什么它会崩溃,但保持最新的保存分数。 上面提到的代码如下所示:

MainActivity:

Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {public void run() {if (Game.getGameOver())
            saveScore(Game.getScore());
        timerHandler.postDelayed(this, 1);
    } };
 public void saveScore(int score)

{
    if (score>getSharedPreferences("myPrefsKey", 0).getInt("key",0))
    {SharedPreferences prefs = getSharedPreferences("myPrefsKey", 0);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt("key", score);
    editor.commit();}
    scoreTextView.setText(getSharedPreferences("myPrefsKey", 0).getInt("key",0));
}
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Game= (Game)findViewById(R.id.Game_canvas);
        timerTextView = (TextView) findViewById(R.id.text1);
        scoreTextView = (TextView) findViewById(R.id.text2);
        scoreTextView.setText(getSharedPreferences("myPrefsKey", 0).getInt("key", 0)
                    timerHandler.postDelayed(timerRunnable, 0);
    }

GameClass(扩展视图)

private boolean gameOver=false;

public boolean getGameOver()
{


    if (condition) {
        gameOver = true;
    }
    else
    gameOver=false;
    return gameOver;
}
public int getScore()
{

    return score;
}

非常感谢!!!

编辑:我再次运行它,这是以下消息

android.content.res.Resources$NotFoundException: String resource ID #0xbb8
        at android.content.res.Resources.getText(Resources.java:245)
        at android.widget.TextView.setText(TextView.java:4042)
        at nocompany.game.MainActivity.saveScore(MainActivity.java:50)
        at nocompany.game.MainActivity$1.run(MainActivity.java:78)
        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:5105)
        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:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
        at dalvik.system.NativeStart.main(Native Method)\

我怀疑它可能只是没有在strings.xml中为SharedPreferences定义String。我会检查一下。 NVM。它没有用。

编辑:错误是尝试将int传递给字符串。

0 个答案:

没有答案