简体   繁体   English

无法启动活动ComponentInfo错误?

[英]Unable to start activity ComponentInfo Error?

I'm trying to duplicate the Android Game Services example "Type A Number Challenge" in my own game. 我正在尝试在自己的游戏中复制Android Game Services示例“ Type A Number Challenge”。 It works fine until I log out and then back in, I get an the following error: 在我注销然后重新登录之前,它工作正常,出现以下错误:

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)

I've looked at all the places where there is a problem, such as MainMenuFragment at line 49 and 62: 我查看了所有有问题的地方,例如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);
    }

And TestGame06 at line 518: 在第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 ( ) ;
}

I'm lost on this error, if anyone has some advise or how I can troubleshoot futher, please speak up! 我对这个错误感到迷失,如果有人提出建议或我如何进一步解决问题,请说出来! Thank you. 谢谢。

It got a NPE on line MainMenuFragment.updateUi(MainMenuFragment.java:62). 它在MainMenuFragment.updateUi(MainMenuFragment.java:62)行上有一个NPE。 Which line is that in your snippet? 您的摘要中哪行? Some value on this line is null and then used eg xxx.setVisibility(...) . 此行上的某些值为null,然后使用,例如xxx.setVisibility(...)

You can set a breakpoint on this line or set an exception breakpoint for NPE, then use the debugger to see what's null. 您可以在此行上设置断点或为NPE设置异常断点,然后使用调试器查看无效值。

Or you can split the line into smaller statements so that only one value could be null in each statement. 或者,您可以将行拆分为较小的语句,以便每个语句中只有一个值可以为null。

Or you could split the statement and add null checks, then run the test again. 或者,您可以拆分语句并添加空检查,然后再次运行测试。

The basic idea is to form hypotheses (in this case, various values that could be null) then test them. 基本思想是形成假设(在这种情况下,可能为空的各种值)然后对其进行检验。

It seems that when findViewById(R.id.sign_in_bar) is called on line 62, it returns a null object, resulting in a NullPointerException when setVisibility() is called. 似乎在第62行调用findViewById(R.id.sign_in_bar)时,它返回一个空对象,并在调用setVisibility()时导致NullPointerException。 Make sure that you're referring to the right id in your res folder. 确保您在res文件夹中引用的是正确的ID。

Like mentioned in the first answer, a good idea would be to separate consecutive statements and ensure which method returns the null reference. 就像第一个答案中提到的那样,一个好主意是分离连续的语句并确保哪个方法返回空引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM