1

我正在使用以下代码。

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)
4

3 回答 3

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)

从上面的错误很明显,错误是由于任何 areaList[i] 为空......见第 394 行

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

  • 在迭代将光标设置为第一个 cursor.moveToFirst() 之前。

.

 cursor.moveToFirst();
        do {


        } while (cursor.moveToNext());
于 2012-06-27T18:50:42.040 回答
0

resultSet.moveToNext()- 移动到下一个,所以我猜你只是跳过数据库中的第一个项目(即使光标包含正确数量的项目 - 50)。

尝试从前置条件切换while(resultSet.moveToNext()) {}到后置条件do {} while(resultSet.moveToNext())

于 2012-06-27T18:51:29.723 回答
0

我认为您在处理该记录之前通过调用 moveToNext 跳过了一条结果记录。

假设您的查询仅返回 50 条记录,请尝试以下更改:

public String[] getAllAreas()
    {
        .....
        resultSet.moveToFirst();
        while(!resultSet.isAfterLast())
        {
            areasList[i] = resultSet.getString(resultSet.getColumnIndex("areaName"));
            i++;
            resultSet.moveToNext();
        }
        .........
    }
于 2012-06-27T19:00:18.863 回答