简体   繁体   中英

How to find root cause of NullPointerException / RuntimeException

Google Play Games Services crashses my game when I log out and back in (using GameHelper). I've been through every line of code and I cannot find a single problem (There is no crash when I'm NOT using Google Game Services).

But when I run my game, with BaseGameUtils library, and using GameHelper to log out, when I log back in the Android app crashes everytime with this error:

I've been troubleshooting this crash for 3 days and I'm at a complete loss. Any advice how to troubleshoot or debug this error is appreciated.

09-10 13:51:03.419: E/AndroidRuntime(9463): FATAL EXCEPTION: main
09-10 13:51:03.419: E/AndroidRuntime(9463): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nurfacegames.testgame07/com.nurfacegames.testgame07.TestGame07}: java.lang.NullPointerException
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.ActivityThread.access$1500(ActivityThread.java:124)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.os.Looper.loop(Looper.java:130)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.ActivityThread.main(ActivityThread.java:3806)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at java.lang.reflect.Method.invokeNative(Native Method)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at java.lang.reflect.Method.invoke(Method.java:507)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at dalvik.system.NativeStart.main(Native Method)
09-10 13:51:03.419: E/AndroidRuntime(9463): Caused by: java.lang.NullPointerException
09-10 13:51:03.419: E/AndroidRuntime(9463):     at com.nurfacegames.testgame07.MainMenuFragment.updateUi(MainMenuFragment.java:61)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at com.nurfacegames.testgame07.MainMenuFragment.onStart(MainMenuFragment.java:48)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.support.v4.app.Fragment.performStart(Fragment.java:1484)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1866)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:568)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at com.google.example.games.basegameutils.BaseGameActivity.onStart(BaseGameActivity.java:110)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at com.nurfacegames.testgame07.TestGame07.onStart(TestGame07.java:522)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.Activity.performStart(Activity.java:3871)
09-10 13:51:03.419: E/AndroidRuntime(9463):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1669)

Here is the MainMenuFragment.class (which is from Google):

public class MainMenuFragment extends Fragment implements OnClickListener {

    String mGreeting = "Hello, anonymous user (not signed in)";

    public interface Listener {
        public void onSignInButtonClicked();
        public void onSignOutButtonClicked();
    }

    Listener mListener = null;
    boolean mShowSignIn = true;
    boolean mShowScreenMenu = true;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_mainmenu, container, false);
        final int[] CLICKABLES = new int[] {
                R.id.sign_in_button, R.id.sign_out_button
        };
        for (int i : CLICKABLES) {
            v.findViewById(i).setOnClickListener(this);
        }
        return v;
    }

    public void setListener(Listener l) {
        mListener = l;
    }

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

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.sign_in_button:
            mListener.onSignInButtonClicked();
            break;
        case R.id.sign_out_button:
            mListener.onSignOutButtonClicked();
            break;
        }
    }

This is TestGame07.java line 522 (super.onStart):

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

找不到3个视图之一(R.id.sign_in_bar或R.id.sign_out_bar或R.id.screen_menu,我看不到行号,所以我不知道是哪一个),因此setVisibility( )方法引用了null对象,它生成NullPointException错误。

In this program the NullPointerException was caused due to the updateUi method

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

A null pointer exception is always raised when any of field or variable is holding a null value and we try to use them in our code

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