2012-08-22 91 views
0

我讀了this教程,並使用它的代碼在我的遊戲中實現ScoreLoop服務。並提交分數工作正常,但顯示其他Android活動或簡單的Toast消息與NullPointer錯誤崩潰。如何在libGDX中顯示Android Toast消息(運行其他Activity)?

墜毀行:

Intent intent = new Intent(mContext, EntryScreenActivity.class); 

和:

Toast.makeText(mContext, "Refreshing scores", Toast.LENGTH_SHORT).show(); 

在調試模式下我看到mContext不爲空

哪裏是錯誤,應該如何解決?

的logcat:

08-22 17:54:27.329: E/AndroidRuntime(15468): java.lang.NullPointerException 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.content.ContextWrapper.getResources(ContextWrapper.java:81) 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.widget.Toast.<init>(Toast.java:92) 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.widget.Toast.makeText(Toast.java:233) 08-22 17:54:27.329: E/AndroidRuntime(15468): at com.masterofcode.android.slingshot.ActionResolverAndroid$3.run(ActionResolverAndroid.java:46) 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.os.Handler.handleCallback(Handler.java:605) 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.os.Handler.dispatchMessage(Handler.java:92) 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.os.Looper.loop(Looper.java:137) 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.app.ActivityThread.main(ActivityThread.java:4424) 08-22 17:54:27.329: E/AndroidRuntime(15468): at java.lang.reflect.Method.invokeNative(Native Method) 08-22 17:54:27.329: E/AndroidRuntime(15468): at java.lang.reflect.Method.invoke(Method.java:511) 08-22 17:54:27.329: E/AndroidRuntime(15468): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 08-22 17:54:27.329: E/AndroidRuntime(15468): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 08-22 17:54:27.329: E/AndroidRuntime(15468): at dalvik.system.NativeStart.main(Native Method) 

======================================================================== 

public class ActionResolverAndroid implements ActionResolver { 
    Handler uiThread; 
    BumbleAndroid mContext; //Your class here which extends AndroidApplication 
    ScoreloopHandler handler; 

    public ActionResolverAndroid(BumbleAndroid mContext) { 
     uiThread = new Handler(); //This binds the handler to the "main" thread, see documentation of handler 
     this.mContext = mContext; 
     handler = new ScoreloopHandler(mContext); 
    } 
} 

這樣,我越來越mContext

+1

貌似mContext爲空。 – nullpotent

+0

但是mContext恰好不爲空 –

+0

你可以顯示你在哪裏設置mContext,我懷疑iccthedral是正確的,它沒有設置。你還有在清單中宣佈你的活動是正確的嗎? –

回答

0

的問題是錯誤的mContext。

正確的代碼是:

public class MyGameAndroidActivity extends AndroidApplication implements 
     ActionResolver { 
public void showScoreloop() { 
     Intent intent = new Intent(this, EntryScreenActivity.class); 
     this.startActivity(intent); 
    } 
}