阅读偏好值

时间:2012-06-15 18:47:15

标签: java android

我正在研究android并试图从我的偏好文件中读取一个值。 但是当我跑步时,一个消息出现在模拟器中 应用程序意外停止 这是我的首选项文件名pref.and是inres / xml

<?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <CheckBoxPreference android:defaultValue="false"
             android:key="@string/first"/>
    </PreferenceScreen>

这是我重新评估价值的主要活动。

package code.finalwork;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

public class FinalWorkActivity extends Activity {
    private String pref_file="pref.xml";
EditText text=(EditText) findViewById(R.id.editText1);
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SharedPreferences pref=getSharedPreferences(pref_file,Context.MODE_PRIVATE);
       Boolean val= pref.getBoolean("firstuse", true);
       if(val)
       {
            text.setText("first use");
       }
       else
       {
           text.setText("Not first use");
       }


    }
}

请有人弄清楚问题。

3 个答案:

答案 0 :(得分:3)

在setContentView之后初始化onCreate函数内的文本(即EditText)。

setContentView(R.layout.main);

之后加上这一行
EditText text=(EditText) findViewById(R.id.editText1);

答案 1 :(得分:1)

以下是您可以自己发现类似问题的答案。转到window-&gt; showview-&gt; logcat

点击红色的行:

  

E / AndroidRuntime(333):at com.test.testing.YourActivityName。(TesterActivity.java:11)​​06-15 11:59:05.616:

你会看到红色:

06-15 11:59:05.526: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception     (group=0x40015560)
06-15 11:59:05.616: E/AndroidRuntime(333): FATAL EXCEPTION: main
06-15 11:59:05.616: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.test.testing/com.test.testing.TesterActivity}: java.lang.NullPointerException
06-15 11:59:05.616: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
06-15 11:59:05.616: E/AndroidRuntime(333):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-15 11:59:05.616: E/AndroidRuntime(333):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-15 11:59:05.616: E/AndroidRuntime(333):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-15 11:59:05.616: E/AndroidRuntime(333):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 11:59:05.616: E/AndroidRuntime(333):  at android.os.Looper.loop(Looper.java:123)
06-15 11:59:05.616: E/AndroidRuntime(333):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-15 11:59:05.616: E/AndroidRuntime(333):  at java.lang.reflect.Method.invokeNative(Native Method)
06-15 11:59:05.616: E/AndroidRuntime(333):  at java.lang.reflect.Method.invoke(Method.java:507)
06-15 11:59:05.616: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-15 11:59:05.616: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-15 11:59:05.616: E/AndroidRuntime(333):  at dalvik.system.NativeStart.main(Native Method)
06-15 11:59:05.616: E/AndroidRuntime(333): Caused by: java.lang.NullPointerException
06-15 11:59:05.616: E/AndroidRuntime(333):  at android.app.Activity.findViewById(Activity.java:1647)
06-15 11:59:05.616: E/AndroidRuntime(333):  at com.test.testing.TesterActivity.<init>(TesterActivity.java:11)
06-15 11:59:05.616: E/AndroidRuntime(333):  at java.lang.Class.newInstanceImpl(Native Method)
06-15 11:59:05.616: E/AndroidRuntime(333):  at java.lang.Class.newInstance(Class.java:1409)
06-15 11:59:05.616: E/AndroidRuntime(333):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-15 11:59:05.616: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
06-15 11:59:05.616: E/AndroidRuntime(333):  ... 11 more

答案 2 :(得分:0)

试试这个:

public class FinalWorkActivity extends Activity {
    private String pref_file="pref.xml";
    EditText text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text = (EditText) findViewById(R.id.editText1);
        SharedPreferences pref=getSharedPreferences(pref_file,Context.MODE_PRIVATE);
        Boolean val= pref.getBoolean("firstuse", true);
        if(val) {
        text.setText("first use");
        } else {
        text.setText("Not first use");
        }
    }
}

如果没有修复,请发布错误日志。

编辑:请记住,以便保存您需要做的偏好:

SharedPreferences pref = getSharedPreferences(pref_file,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();          
editor.putBoolean("firstuse", authToken);
editor.commit();