I just want to restore the saved preferences, but an error occurs.
Login Class:
SharedPreferences.Editor editor = settings.edit();
editor.putString("session_id", session);
editor.commit();
List Class:
editor.putInt("channel_id", map.get(lv2.getItemAtPosition(arg2).toString()));
editor.commit();
Main Class:
public class MainScreen extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView tv = (TextView) findViewById(R.id.mainscreen);
final SharedPreferences settings = getSharedPreferences("pref", 0);
String tmp= settings.getString("session_id", "Error") + "ID: " +String.valueOf(settings.getInt("channel_id", -1));
tv.setText(tmp);
setContentView(R.layout.mainscreen);
}
}
LogCat:
02-01 21:06:56.621: ERROR/ApplicationContext(25035): Couldn't rename file /dbdata/databases/android.skiptvad/shared_prefs/pref.xml to backup file /dbdata/databases/android.skiptvad/shared_prefs/pref.xml.bak
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): FATAL EXCEPTION: main
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.skiptvad/android.skiptvad.MainScreen}: java.lang.NullPointerException
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): at android.os.Looper.loop(Looper.java:123)
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-01 21开发者_如何转开发:06:56.727: ERROR/AndroidRuntime(25035): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): at java.lang.reflect.Method.invoke(Method.java:521)
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): at dalvik.system.NativeStart.main(Native Method)
02-01 21:06:56.727: ERROR/AndroidRuntime(25035): Caused by: java.lang.NullPointerException
Are you using a galaxy S? If so look at the path its trying to save to... "/dbdata" rather than "/data". It is a problem on Samsung.
http://code.google.com/p/android/issues/detail?id=14359
The error is caused when you try to access the TextView before calling setContentView.
setContentView(R.layout.mainscreen);
must be before
TextView tv = (TextView) findViewById(R.id.mainscreen);
or else
tv.setText(tmp);
will cause a null pointer exception.
精彩评论