无法启动活动ComponentInfo错误?

时间:2014-09-09 21:49:18

标签: java android google-play-games shiva3d

我正在尝试在我自己的游戏中复制Android游戏服务示例“键入数字挑战”。它工作正常,直到我退出然后重新进入,我得到以下错误:

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 ( ) ;
}

我迷失了这个错误,如果有人有一些建议或我如何进一步排除故障,请说出来!谢谢。

2 个答案:

答案 0 :(得分:1)

它在MainMenuFragment.updateUi(MainMenuFragment.java:62)上有一个NPE。您的代码段中包含哪一行?此行上的某些值为null,然后使用例如xxx.setVisibility(...)

您可以在此行上设置断点或为NPE设置异常断点,然后使用调试器查看什么是null。

或者您可以将该行拆分为较小的语句,以便每个语句中只有一个值为null。

或者您可以拆分语句并添加空检查,然后再次运行测试。

基本思想是形成假设(在这种情况下,各种值可以为null)然后测试它们。

答案 1 :(得分:1)

似乎在第62行调用findViewById(R.id.sign_in_bar)时,它返回一个null对象,当调用setVisibility()时会导致NullPointerException。确保您在res文件夹中引用了正确的ID。

如第一个答案中提到的,一个好主意是分隔连续的语句并确保哪个方法返回空引用。