简体   繁体   中英

Unable to start activity ComponentInfo Error?

I'm trying to duplicate the Android Game Services example "Type A Number Challenge" in my own game. 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:

 @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:

//------------------------------------------------------------------
// @@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). Which line is that in your snippet? Some value on this line is null and then used eg 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.

Or you can split the line into smaller statements so that only one value could be null in each statement.

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. Make sure that you're referring to the right id in your res folder.

Like mentioned in the first answer, a good idea would be to separate consecutive statements and ensure which method returns the null reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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