当滚动到达列表末尾时,Android Spinner会导致App崩溃

时间:2012-03-06 14:59:06

标签: android android-layout android-listview android-spinner

在我的一个活动中,我使用了 Spinner 。我使用一组 SpinnerCountry 类对象填充它...其定义如下:

class SpinnerCounry {
    public SpinnerCounry(String spinnerText, String value) {
        this.spinnerText = spinnerText;
        this.value = value;
    }

    public String getSpinnerText() {
        return spinnerText;
    }

    public String getValue() {
        return value;
    }

    public String toString() {
        return spinnerText;
    }

    String spinnerText;
    String value;
}

我正在以下函数中绑定 Spinner

private void setCountriesSpinner() {

    //create array of countries!
    final SpinnerCounry items[] = new SpinnerCounry[233];
    items[0] = new SpinnerCounry("Abkhazia", "207");
    items[1] = new SpinnerCounry("Afghanistan", "1");
    items[2] = new SpinnerCounry("Albania", "103");
    items[3] = new SpinnerCounry("Algeria", "2");
              ....
              ....
              ....
    items[230] = new SpinnerCounry("Zambia", "154");
    items[231] = new SpinnerCounry("Zimbabwe", "155");

    ArrayAdapter<SpinnerCounry> adapter = new ArrayAdapter<SpinnerCounry>(this, android.R.layout.simple_spinner_item, items);
         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    Spinner s = (Spinner) findViewById(R.id.spinner);

    s.setAdapter(adapter);      

    s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {

            //get the selected country's id to use later
            _countryId = items[position].getValue();

        }

        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
}

这很好用......加载spinner&amp;中的国家点击/点按时选择正确的国家/地区ID。

但是当我将Spinner滚动到最后一个国家时,它会崩溃应用程序。我看了看LogCat,但没有弄错......

LogCat如下:

03-06 19:39:04.004: E/AndroidRuntime(1179): FATAL EXCEPTION: main
03-06 19:39:04.004: E/AndroidRuntime(1179): java.lang.NullPointerException
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:398)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:419)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:543)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:539)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.AbsListView.obtainView(AbsListView.java:1949)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1156)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.ListView.makeAndAddView(ListView.java:1756)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.ListView.fillDown(ListView.java:656)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.ListView.fillGap(ListView.java:620)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4408)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:3687)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.os.Handler.handleCallback(Handler.java:587)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.os.Looper.loop(Looper.java:132)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at android.app.ActivityThread.main(ActivityThread.java:4025)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at java.lang.reflect.Method.invoke(Method.java:491)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-06 19:39:04.004: E/AndroidRuntime(1179):     at dalvik.system.NativeStart.main(Native Method)

非常感谢任何帮助。感谢

2 个答案:

答案 0 :(得分:2)

您的阵列包含233个条目。但是最后一个条目[232]是空的,即null。这就是让您的应用程序崩溃的原因。

我建议使用ArrayList而不是当前的固定数组方法。

答案 1 :(得分:0)

您已创建了233个元素的数组,但只插入了232个元素。

也许,当您尝试访问索引232处的元素时,会出现空指针异常。