0

I have published on google play my app. Google is telling me that my app, when was being used by users, crashed 2 times. I tested my app a lot of times, but i never get that error, and i don't know how to simulate that error.

java.lang.RuntimeException: Unable to resume activity {com.mdp.slotmachine/com.mdp.slotmachine.CoverActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2444)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4429)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.ContextImpl.startService(ContextImpl.java:1088)
at android.content.ContextWrapper.startService(ContextWrapper.java:359)
at com.mdp.slotmachine.CoverActivity.onResume(Unknown Source)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
at android.app.Activity.performResume(Activity.java:4539)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434)
... 12 more

The error seems to be in CoverActivity class, inside onResume method. As you can see i don't do anything in onResume method.

  @Override
  protected void onResume() {
  super.onPause();

  //check if i'm publishing the app on samsung store
        if(MainActivity.samsung){   

          // here i check if my app was properly downloaded from samsung store by using Zirconia.
         // BUT I NEVER GET HERE; BECAUSE THE PREVIUOS VARIABLE (MainActivity.samsung) IS ALWAYS FALSE.
            if(!MainActivity.licensaSamsung){
                    finish();
            }
        }
  }
MDP
  • 4,177
  • 21
  • 63
  • 119
  • Is super.onPause() a typo? Shouldnt it be super.OnResume() – Lazy Ninja Feb 04 '13 at 07:11
  • As others already noted, calling `super.onPause()` in many life cycle callbacks of your Activity is wrong. However, I'm not sure it causes the NullPointerException, especially considering the stacktrace you posted. I have a question regarding `MainActivity.samsung`. I suspect it might be static. The question is, in which activity are you referring to it? It may not be available at that point in time, despite being static. – class stacker Feb 04 '13 at 07:34
  • I think you are right. I still have the same error, even after solving the `super.onPause()` error. MainActivity.samsung is a final static boolean variable. It works this way: my app starts with an Activity Class called MainActivity. In this class i create the final static boolean variable "samsung". In the onCreate() method of MainActivity , i open another activity, with an intent, called CoverActivity. And in CoverActivity i "use" this variable. When onResume() in CoverActivity is called, the samsung variable in MainActivity doesn't exsist anymore? – MDP Feb 04 '13 at 19:11
  • @MatteoDepasquali I have provided an answer now which should fix your issue. The answer you currently selected contains valid facts; however, it's not the answer to your current problem. – class stacker Feb 05 '13 at 07:50

2 Answers2

0

Instead of calling super.onPause(); use super.onResume();

 @Override
protected void onResume() {
    //super.onPause();check if i'm publishing the app on samsung store
    super.onResume();// call this method
if(MainActivity.samsung){    
            // here i check if my app was properly downloaded from samsung store by using Zirconia. BUT I NEVER GET HERE; BECAUSE THE PREVIUOS VARIABLE (MainActivity.samsung) IS ALWAYS FALSE.
        if(!MainActivity.licensaSamsung){
            finish();
        }
    }
}
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
  • Oh thank you. This is my first app, so i'm not good at developing android app. I just notice that I put `super.onPause()` everywhere! inside `onPause()`, `onResume()`, `onStop()` and `onDestroy()` method. But i don't remember why i did it. Maybe i read it somewhere a reason to do it, or maybe i just made a big mistake. Is there a reason to use super.Onpause() the way i did, or i just made a huge mistake? – MDP Feb 04 '13 at 07:21
  • @MatteoDepasquali You made a huge mistake. – class stacker Feb 04 '13 at 07:30
0

As per our discussion related to your question, your approach of keeping a final static boolean samsung; in your start Activity is fatal and wrong. This does not work on Android.

Although there are reasons when you shouldn't use static variables, I think that's fine for such purposes as startup analysis of environment aspects.

The right way to do this on Android is to derive an Application class, perform the analysis there and store it in variables there. Typical design considerations apply, but here's a basic scheme which may work for you.

public final class MyApplication extends Application {
    public static boolean isSamsung;
    @Override
    // Application.onCreate() is called on the main thread before your first Activity
    public void onCreate() {
        isSamsung = myFancyComputation();
    }
 }

An Instance of MyApplication is guaranteed to survive as long as you App does not get killed or is terminated. Please note that this may occur erlier or much later than you think; please try to fully understand the Application and Activity lifecycles before you try to rely on them.

With this static singleton approach, it's super easy to access Application.isSamsung; however, you could also make it non-static and access it via the Activity's means to find its Application context.

class stacker
  • 5,357
  • 2
  • 32
  • 65
  • Thank you, then this is the way i have to use static variables on android? This means i can define a final static variable only in a singleton class? – MDP Feb 05 '13 at 10:26
  • @MatteoDepasquali Sorry about the `final` -- not working in this case, because it is only initialized in `onCreate()`. It could be final if you can already initialize it in the ?MyApplication` constructor, but I'm not sure what kind of services/information would be accessible at that point in time, so `onCreate()` is the better choice. You may want to read more opinions on this, like http://stackoverflow.com/questions/3826905/singletons-vs-application-context-in-android – class stacker Feb 05 '13 at 10:34