我目前有以下SharedPreferences
设置如下:
public class MainActivity extends Activity implements OnClickListener {
Button openedLockers, closedLockers, checkout, admin;
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
我还有另一个SharedPreferences
用于其他内容:
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
}
}
然后我称之为:
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;
}
但是当我运行应用程序时,我得到以下内容:
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
有什么想法吗?
答案 0 :(得分:3)
移动此初始化
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
到onCreate()
。
初始化成员变量时,不能将活动用作Context
。