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 ( ) ;
}
Selzier
  • 101
  • 1
  • 6
  • 19
  • Usually is the next line after `Caused by: java.lang.NullPointerException`. In your case, it is: `at com.nurfacegames.testgame07.MainMenuFragment.updateUi(MainMenuFragment.java:61)`. – Luiggi Mendoza Sep 10 '14 at 19:59
  • 1
    what did you find when you looked at this line `MainMenuFragment.updateUi(MainMenuFragment.java:61)` – tyczj Sep 10 '14 at 19:59
  • The log said the NullPointerException is caused by this line: `com.nurfacegames.testgame07.MainMenuFragment.updateUi(MainMenuFragment.java:61)`. Check the code for the bug. – hfann Sep 10 '14 at 20:00
  • This is code from Google, which is from here: https://github.com/playgameservices/android-basic-samples – Selzier Sep 10 '14 at 20:01
  • Did you get a ready-to-use JAR(s) or have you compiled their source yourself? – PM 77-1 Sep 10 '14 at 20:02
  • I had to compile their source, I can't understand why there is a problem in updateUi, when it's Google's code and it works with their sample project outside of my game. – Selzier Sep 10 '14 at 20:04
  • getActivity().findViewById(R.id.sign_in_bar).setVisibility(mShowSignIn ? – Selzier Sep 10 '14 at 20:05
  • 1
    In `updateUi`, you have null checks for `getActivity` and for `tv` but not for the next 3 views. Add them. – Simas Sep 10 '14 at 20:05
  • Sign in Bar never has a problem before, but my activity gets stopped, so that Google's Sign-In screen shows, then when my activity resumes after you pick a Google Account, the crash occurs. – Selzier Sep 10 '14 at 20:06
  • 1
    We need to see `TestGame07.java:522` – Motomotes Sep 10 '14 at 20:07
  • Are you using Eclipse? If so, have you tried to **clean** your project. – PM 77-1 Sep 10 '14 at 20:08
  • 522 is super.onStart: – Selzier Sep 10 '14 at 20:08
  • @Override protected void onStart ( ) { super.onStart ( ) ; } – Selzier Sep 10 '14 at 20:09
  • Clean, refresh, restart eclipse, etc =/ – Selzier Sep 10 '14 at 20:09
  • Can you cause this error at will following the same scenario multiple times?? – PM 77-1 Sep 10 '14 at 20:10
  • Any chance you're missing any `XML` files in your project? – PM 77-1 Sep 10 '14 at 20:11
  • Yes, I can reproduce anytime by starting the game, which signs you in automatically, then logging out of Google Game services, then when I log back in, the Google Account selection screen is shown (different activity), and when my Activity resumes after that > This crash occurs. – Selzier Sep 10 '14 at 20:12
  • No, I have the exact same XML files as I did before, in a previous version of Google Play Game Services, when it work flawlessly. This error is since the API has been updated. – Selzier Sep 10 '14 at 20:14
  • Have you tried to ask GitHub for differences between "stable" amd "updated" versions? – PM 77-1 Sep 10 '14 at 20:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61005/discussion-between-pm-77-1-and-selzier). – PM 77-1 Sep 10 '14 at 20:16

2 Answers2

0

One of the 3 views (R.id.sign_in_bar or R.id.sign_out_bar or R.id.screen_menu, i don't see line numbers so i can't tell which one) is not found, so one of the setVisibility() methods refers to the null object, it generates NullPointException error.

skrzatswat
  • 59
  • 5
  • Hi @skrzatswat, Welcome to Stackoverflow. Please take a [tour](http://stackoverflow.com/tour) to learn how to properly format your questions. If you could post the actual xml code, logcat and lines defined in the logcat error than you are more likely to receive help. – RyPope Sep 10 '14 at 20:36
0

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