Question

I currently have the following SharedPreferences set up like the following:

public class MainActivity extends Activity implements OnClickListener {

    Button openedLockers, closedLockers, checkout, admin;
    SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();

I also have another SharedPreferences for something else:

 Private void runOnce() {
        // TODO Auto-generated method stub
        SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
        mboolean = settings.getBoolean("FIRST_RUN", false);
        if (!mboolean) {
            // do the thing for the first time
            settings = getSharedPreferences("PREFS_NAME", 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("FIRST_RUN", true);
            editor.commit();
            prepopulate();
        } else {
            // other time your app loads
        }
    }

Then I call it:

public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case R.id.setIp:
            AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
                    .create();
            alert.setTitle("Enter new ip");
            alert.setMessage("Enter the new ip");
            final EditText input = new EditText(this);
            alert.setView(input);
            alert.setButton("submit", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    String value = input.getText().toString();
                    editor.putString("IP", value);
                    editor.commit();
                    SharedPreferences preferences = getPreferences(MODE_PRIVATE);
                    String tut = preferences.getString("IP", "");
                    ip = tut;
                }
            });
            break;
        }
        return false;
    }

However when I run the application I get the following:

04-07 12:49:30.085:W/dalvikvm(12538): threadid=1: thread exiting with uncaught exception (group=0x41679c08) 
04-07 12:49:30.085: E/AndroidRuntime(12538): FATAL EXCEPTION: main 
04-07 12:49:30.085: E/AndroidRuntime(12538): Process: com.samsunglockercenter, PID: 12538 
04-07 12:49:30.085: E/AndroidRuntime(12538): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.samsunglockercenter/com.samsunglockercenter.MainActivity}: java.lang.NullPointerException 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at android.app.ActivityThread.access$800(ActivityThread.java:164) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at android.os.Handler.dispatchMessage(Handler.java:102) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at android.os.Looper.loop(Looper.java:157) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at android.app.ActivityThread.main(ActivityThread.java:5341) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at java.lang.reflect.Method.invokeNative(Native Method) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at java.lang.reflect.Method.invoke(Method.java:515) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1259)    04-07 12:49:30.085: E/AndroidRuntime(12538):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at dalvik.system.NativeStart.main(Native Method) 
04-07 12:49:30.085: E/AndroidRuntime(12538): Caused by: java.lang.NullPointerException 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at android.app.Activity.getLocalClassName(Activity.java:4668) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at android.app.Activity.getPreferences(Activity.java:4701) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at com.samsunglockercenter.MainActivity.<init>(MainActivity.java:31) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at java.lang.Class.newInstanceImpl(Native Method) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at java.lang.Class.newInstance(Class.java:1208) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at android.app.Instrumentation.newActivity(Instrumentation.java:1079) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215) 
04-07 12:49:30.085: E/AndroidRuntime(12538):    ... 11 more

Any ideas?

Was it helpful?

Solution

Move this initialization

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();

to your onCreate().

You cannot use an activity as a Context when the member variables are initialized.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top