0

使用 sharedpreferences 我运行此方法:

private Button button1;
private Button button2;
private TextView textView1;

@Override
public void onCreate(Bundle savedInstanceState){ 
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
checkIndex();

// 如果没有,这里保存首选项 // 在程序第二次启动后,checkIndex(); 去那次崩溃

public void checkIndex(){
    SharedPreferences prefs = getSharedPreferences(PRIVATE_PREF, 0);
    String index = prefs.getString("scelta", "");
    if (index == ""){
 index();
    }
    else {

        String scelta = prefs.getString("scelta", "");
        int a = prefs.getInt("arraystart", 0);
        int b = prefs.getInt("arrayend", 0);

        int size = prefs.getInt(prog +"_size", 0);
        String prog[] = new String[size];
        for(int i=0; i<prog.length; i++)

         prog[i] = prefs.getString(prog + "_" + i, null);

        String titolo = prefs.getString("titolo", "");
        int x = prefs.getInt("x", 0);

        textView1.setText(""+a+" "+b+" "+titolo);

我有一个 NullPointer 异常,textView1但我不明白为什么!(我花了很多时间但什么都没有)......为什么?谢谢!

日志猫:

11-30 21:49:42.806: E/AndroidRuntime(3739): FATAL EXCEPTION: main
11-30 21:49:42.806: E/AndroidRuntime(3739): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.perledisaggezza/com.example.perledisaggezza.MainActivity}: java.lang.NullPointerException
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.os.Looper.loop(Looper.java:137)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at java.lang.reflect.Method.invokeNative(Native Method)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at java.lang.reflect.Method.invoke(Method.java:511)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at dalvik.system.NativeStart.main(Native Method)
11-30 21:49:42.806: E/AndroidRuntime(3739): Caused by: java.lang.NullPointerException
11-30 21:49:42.806: E/AndroidRuntime(3739):     at com.example.perledisaggezza.MainActivity.checkIndex(MainActivity.java:480)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at com.example.perledisaggezza.MainActivity.onCreate(MainActivity.java:149)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.Activity.performCreate(Activity.java:5008)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-30 21:49:42.806: E/AndroidRuntime(3739):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-30 21:49:42.806: E/AndroidRuntime(3739):     ... 11 more
11-30 21:51:08.107: E/Trace(3796): error opening trace file: No such file or directory (2)
4

2 回答 2

1

你在做checkIndex() 之前调用textView1 = (TextView)findViewById(R.id.textView1);你需要先初始化 textView1 然后你可以使用它,否则checkIndex()使用空引用。因此,您需要在textView1 = (TextView)findViewById(R.id.textView1);之前添加checkIndex()

于 2012-11-30T22:00:03.587 回答
0

看看你的 onCreate(...) 方法。好像没写

setContentView(R.layout.some_layout_file)

包含一个带有 textView1 id 的 TextView。或者你应该使用

TextView textView1 = (TextView) findViewById(R.id.textView1)
于 2012-11-30T21:52:45.450 回答