我正在使用以下代码。
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)