-2

我正在使用 android 并尝试从我的首选项文件中读取一个值。但是当我运行时,模拟器 中出现一条消息。应用程序意外停止 这里是我的首选项文件名 pref。它是 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");
       }


    }
}

请有人找出问题所在。

4

3 回答 3

3

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

把这条线放在后面setContentView(R.layout.main);

EditText text=(EditText) findViewById(R.id.editText1);
于 2012-06-15T18:50:49.603 回答
1

Here is how you could discover the answer to something like this yourself. Go to window->showview->logcat

Click on the line in red:

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

You would see in red:

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
于 2012-06-15T19:02:14.047 回答
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();
于 2012-06-15T18:55:09.027 回答