0

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

2 回答 2

0

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

于 2014-09-10T20:24:16.497 回答
0

在这个程序中,NullPointerException 是由于 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);
}

当任何字段或变量持有空值并且我们尝试在代码中使用它们时,总是会引发空指针异常

于 2014-09-14T10:16:41.867 回答