我在Android上編寫遊戲,這個問題困擾了我。我重寫onPause()以使遊戲在數據不可見時將其數據保存到內部存儲。它工作正常,直到三次或四次按回或回家。我得到這個代替:Nullpointerexception和onPause():一個謎
Thread [<1> main] (Suspended (exception NullPointerException))
Main.onPause() line: 127
Main(Activity).performPause() line: 3842
Instrumentation.callActivityOnPause(Activity) line: 1190
ActivityThread.performPauseActivity(ActivityThread$ActivityRecord, boolean, boolean) line: 3335
ActivityThread.performPauseActivity(IBinder, boolean, boolean) line: 3305
ActivityThread.handlePauseActivity(IBinder, boolean, boolean, int) line: 3288
ActivityThread.access$2500(ActivityThread, IBinder, boolean, boolean, int) line: 125
BinderProxy(ActivityThread$H).handleMessage(Message) line: 2044
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
調試器指出它在這個炸燬:
@Override
public void onPause() {
super.onPause();
Data data = game.writeProtoBuf(); // NullPointerException
try {
//IO stuff
writeProtoBuf實際上並沒有寫任何東西,但僅僅是一個檢索遊戲數據,並把它放入方法數據對象。調試器顯示遊戲爲空,但我沒有看到在關閉遊戲之前我可以正確地與遊戲進行交互的可能性。 :/
嗨,有沒有可能,遊戲對象可以爲空....因爲如果你在'Data data = game.writeProtoBuf(); '意味着它是遊戲是空的... – Dinash
我們需要看到更多的代碼。 –