Android activity stopped unexpectedly

407 Views Asked by At

I am trying to use SharedPreference to store some values and access them later, but don't know why my Activity crashed and giving me this error.

06-12 11:03:40.459    1525-1525/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ets.medecord/com.ets.medecord.SplashActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1680)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
            at android.app.ActivityThread.access$1500(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3835)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
            at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:353)
            at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:348)
            at com.ets.medecord.SplashActivity.<init>(SplashActivity.java:15)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1409)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1672)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
            at android.app.ActivityThread.access$1500(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3835)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
            at dalvik.system.NativeStart.main(Native Method)

Code which I am using in my SplashActivity.java. when I run my app it crashes but if I remove those lines containing SharedPreference.

public class SplashActivity extends ActionBarActivity{
static final int PAUSE_TIME = 2000;
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = pref.edit();
boolean isAppFirstStart = pref.getBoolean("start",true);
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    handler = new Handler();
    getSupportActionBar().hide();

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(PAUSE_TIME);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if(isAppFirstStart) {
                        editor.putBoolean("start",false);
                        editor.commit();
                        startActivity(new Intent(getApplicationContext(), IntroActivity.class));
                        finish();
                    } else {
                        startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                        finish();
                    }
                }
            });
        }
    }).start();
  }
 }
2

There are 2 best solutions below

0
On BEST ANSWER

The problem is with:

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);

Move your shared preference lines to onCreate, like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = pref.edit();
    ...
}

Then it will work.

The reason PreferenceManager.getDefaultSharedPreferences(this) was giving you a null pointer exception, is because this cannot be resolved before onCreate().

0
On

Outside class methods and in static initialization blocks, this is null. It hasn't been initialized yet.

Thus, you can't pass this in such places.

Default constructor vs. inline field initialization