2012-06-27 49 views
1

我正在使用以下代碼。arrayadapter和nullpointer錯誤的意外行爲

mDbHelper.open(); 
     String[] areasList = new String[51]; 
     for (int i = 0; i<51; i ++) 
     { 
      areasList[i] = ""; 
     } 
     areasList = mDbHelper.getAllAreas(); 
     mDbHelper.close(); 

     ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, areasList); 
     spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     chooseArea.setAdapter(spinnerArrayAdapter); 

在調試它顯示了從數據庫中獲取的的String[]所有值。

public String[] getAllAreas() 
    { 
     String[] areasList = new String[51]; 
     String queryString = "SELECT areaName FROM TFTaxi"; 
     Cursor resultSet = mDb.rawQuery(queryString, null); 
     for (int i = 0; i<51; i ++) 
     { 
      areasList[i] = ""; 
     } 
     int i = 1; 
     areasList[0] = "Choose your Area"; 

     while(resultSet.moveToNext()) 
     { 

      areasList[i] = resultSet.getString(resultSet.getColumnIndex("areaName")); 
      i++; 
     } 

     resultSet.close(); 
     return areasList; 
    } 

但隨後給出了上面的錯誤錯誤

06-27 23:40:43.882: E/AndroidRuntime(16467): FATAL EXCEPTION: main 
06-27 23:40:43.882: E/AndroidRuntime(16467): java.lang.NullPointerException 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.Spinner.onMeasure(Spinner.java:285) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.view.View.measure(View.java:12723) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.view.View.measure(View.java:12723) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.view.View.measure(View.java:12723) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.view.View.measure(View.java:12723) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.view.View.measure(View.java:12723) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.view.View.measure(View.java:12723) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.os.Handler.dispatchMessage(Handler.java:99) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.os.Looper.loop(Looper.java:137) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.app.ActivityThread.main(ActivityThread.java:4424) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at java.lang.reflect.Method.invokeNative(Native Method) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at java.lang.reflect.Method.invoke(Method.java:511) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at dalvik.system.NativeStart.main(Native Method) 
+0

你的陣列的初始化代碼調用areasList = mDbHelper.getAllAreas前();沒有效果。你可以刪除它。 除此之外:你是否確認你的數據庫不*包含空值? (你可以通過約束NOT NULL來強制執行)。 – tiguchi

回答

1
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394) 
06-27 23:40:43.882: E/AndroidRuntime(16467): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 

很明顯的錯誤是由於任何areasList的[i]爲空.... see line 394

文本.setText(item.toString());

  • 迭代之前將光標設置爲第一個cursor.moveToFirst()。

cursor.moveToFirst(); 
     do { 


     } while (cursor.moveToNext()); 
+0

不工作的隊友,因爲我已經說過,我的調試器顯示整個51值的字符串,沒有空字符:( –

+0

然後文本(TextView)可能爲空,這也是在394讓我檢查 –

+0

請更新最後的代碼,並告訴哪裏你檢查了所有字符串的51個值,沒有空字符 –

0

resultSet.moveToNext() - 移動到下一個,所以我的猜測是,你只跳過從數據庫中的第一項(即使光標包含的項目的正確數量 - 50)。

嘗試從前提while(resultSet.moveToNext()) {}切換到後置條件do {} while(resultSet.moveToNext())

+0

我的調試器在整個String中顯示無空值[ ] –

+0

@umar:那麼我只是出於想法,對不起.. –

0

我想你通過處理記錄之前調用使用MoveToNext跳過一個結果記錄。

假設你的查詢返回50個記錄嘗試這種變化:

public String[] getAllAreas() 
    { 
     ..... 
     resultSet.moveToFirst(); 
     while(!resultSet.isAfterLast()) 
     { 
      areasList[i] = resultSet.getString(resultSet.getColumnIndex("areaName")); 
      i++; 
      resultSet.moveToNext(); 
     } 
     ......... 
    }