2012-11-30 54 views
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); 

我已經textView1一個空指針異常,我不明白爲什麼! (我花了很多時間但沒有)......爲什麼?謝謝!

的logcat:

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) 
+0

你可以發佈完整的logcat嗎? –

+0

此外,你不能比較字符串==,使用.equals()或檢查長度爲0. –

+0

好吧我會修改(但現在的作品),謝謝。 –

回答

1

要調用checkIndex()textView1 = (TextView)findViewById(R.id.textView1);您需要首先initiatlize textView1,那麼你可以使用它之前,否則checkIndex()正在與空引用。因此,您需要在checkIndex()之前添加textView1 = (TextView)findViewById(R.id.textView1);;

+0

cool A - C !!!我爲你喝了很多啤酒!非常非常感謝! –

+0

然後你需要發佈你的整個代碼,你給我們不完整的信息。 –

+0

沒問題!然而,下一次,發佈整個代碼,你留下了我們想知道究竟發生了什麼的地方。 –

0

看看你的onCreate(...)方法。看起來你沒有寫

setContentView(R.layout.some_layout_file) 

它包含一個帶有textView1 id的TextView。還是應該使用

TextView textView1 = (TextView) findViewById(R.id.textView1) 
+0

我重新檢查:我有相同的代碼(onCreate運行後的textView1,啓動時崩潰checkIndex();) –

+0

您沒有初始化textView1。您添加布局,然後轉到checkIndex()方法而不初始化textView1。在checkIndex()的開頭使用這一行:'textView1 =(TextView)findViewById(R.id.textView1)' –