我正在尝试在我自己的游戏中复制 Android 游戏服务示例“Type A Number Challenge”。在我注销然后重新登录之前它工作正常,我收到以下错误:
09-09 15:23:11.372: E/AndroidRuntime(7438): FATAL EXCEPTION: main
09-09 15:23:11.372: E/AndroidRuntime(7438): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nurfacegames.testgame06/com.nurfacegames.testgame06.TestGame06}: java.lang.NullPointerException
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.app.ActivityThread.access$1500(ActivityThread.java:124)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.os.Looper.loop(Looper.java:130)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.app.ActivityThread.main(ActivityThread.java:3806)
09-09 15:23:11.372: E/AndroidRuntime(7438): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 15:23:11.372: E/AndroidRuntime(7438): at java.lang.reflect.Method.invoke(Method.java:507)
09-09 15:23:11.372: E/AndroidRuntime(7438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-09 15:23:11.372: E/AndroidRuntime(7438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-09 15:23:11.372: E/AndroidRuntime(7438): at dalvik.system.NativeStart.main(Native Method)
09-09 15:23:11.372: E/AndroidRuntime(7438): Caused by: java.lang.NullPointerException
09-09 15:23:11.372: E/AndroidRuntime(7438): at com.nurfacegames.testgame06.MainMenuFragment.updateUi(MainMenuFragment.java:62)
09-09 15:23:11.372: E/AndroidRuntime(7438): at com.nurfacegames.testgame06.MainMenuFragment.onStart(MainMenuFragment.java:49)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.support.v4.app.Fragment.performStart(Fragment.java:1528)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:972)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1906)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:588)
09-09 15:23:11.372: E/AndroidRuntime(7438): at com.google.example.games.basegameutils.BaseGameActivity.onStart(BaseGameActivity.java:110)
09-09 15:23:11.372: E/AndroidRuntime(7438): at com.nurfacegames.testgame06.TestGame06.onStart(TestGame06.java:518)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.app.Activity.performStart(Activity.java:3871)
09-09 15:23:11.372: E/AndroidRuntime(7438): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1669)
我查看了所有有问题的地方,例如第 49 和 62 行的 MainMenuFragment:
@Override
public void onStart() {
super.onStart();
updateUi();
}
public void setGreeting(String greeting) {
mGreeting = greeting;
updateUi();
}
void updateUi() {
if (getActivity() == null) return;
TextView tv = (TextView) getActivity().findViewById(R.id.hello);
if (tv != null) tv.setText(mGreeting);
getActivity().findViewById(R.id.sign_in_bar).setVisibility(mShowSignIn ?
View.VISIBLE : View.GONE);
getActivity().findViewById(R.id.sign_out_bar).setVisibility(mShowSignIn ?
View.GONE : View.VISIBLE);
getActivity().findViewById(R.id.screen_menu).setVisibility(mShowScreenMenu ?
View.VISIBLE : View.GONE);
}
和第 518 行的 TestGame06:
//------------------------------------------------------------------
// @@BEGIN_ACTIVITY_METHODS@@
//------------------------------------------------------------------
@Override
protected void onStart ( )
{
Log.d ( Globals.sApplicationName, "--------------------------------------------" ) ;
Log.d ( Globals.sApplicationName, "Start activity " + Globals.sApplicationName ) ;
Log.d ( Globals.sApplicationName, "--------------------------------------------" ) ;
super.onStart ( ) ;
}
我迷失了这个错误,如果有人有一些建议或者我可以如何进一步排除故障,请说出来!谢谢你。