无法在共享首选项中重命名文件

时间:2011-02-01 20:12:29

标签: android

我只想恢复已保存的偏好设置,但会发生错误。

登录类:

SharedPreferences.Editor editor = settings.edit();
                        editor.putString("session_id", session);
                        editor.commit();

列表类:

editor.putInt("channel_id", map.get(lv2.getItemAtPosition(arg2).toString()));
editor.commit();

主类:

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

2 个答案:

答案 0 :(得分:2)

您使用的是星系S吗?如果是这样,请查看其尝试保存到...“/ dbdata”而不是“/ data”的路径。这是三星的一个问题。

http://code.google.com/p/android/issues/detail?id=14359

答案 1 :(得分:0)

在调用setContentView之前尝试访问TextView时会导致错误。

setContentView(R.layout.mainscreen);

必须在

之前
TextView tv = (TextView) findViewById(R.id.mainscreen);

或者

tv.setText(tmp);

将导致空指针异常。