Listview在Scroll上崩溃

时间:2016-02-24 17:02:07

标签: android listview onitemclicklistener

正如标题所示,当我尝试滚动列表视图时,我的应用程序当前崩溃了。

,当列表视图打开时,一切都按预期工作。但是一旦我向下滚动,它就会崩溃。

列表视图包含在一个对话框中;下面的代码片段显示了我如何初始化对话框和数组适配器:

final Dialog startersDialog = new Dialog(context);
    startersDialog.setContentView(R.layout.starters_dialog);
    startersDialog.setTitle("Select Starter");
    convStarters = (ListView) startersDialog.findViewById(R.id.scenarios);
    greetingsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, chatBot.getGreetingsInputs());
    convStarters.setAdapter(greetingsAdapter);

当我选择列表项时调用的代码:

convStarters.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
                    userInput = adapter.getItemAtPosition(position).toString();
                    System.out.println("User Input Variable is: ");
                    userInputView.setText(userInput);
                    userInputView.setFocusable(false);
                    startersDialog.dismiss();
                }
            }
    );

Dialog XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scenarios"/>

我得到的错误是:

02-24 16:59:16.914  17612-17612/com.example.alex.projectui E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.alex.projectui, PID: 17612
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
        at android.widget.AbsListView.obtainView(AbsListView.java:2823)
        at android.widget.ListView.makeAndAddView(ListView.java:1884)
        at android.widget.ListView.fillDown(ListView.java:713)
        at android.widget.ListView.fillGap(ListView.java:677)
        at android.widget.AbsListView.trackMotionScroll(AbsListView.java:7040)
        at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:6478)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
        at android.view.Choreographer.doCallbacks(Choreographer.java:590)
        at android.view.Choreographer.doFrame(Choreographer.java:559)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5942)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

如果问题相当简单,我道歉,但过去几天我一直在努力解决这个问题,而且我找不到任何明显或易于跟踪的资源来处理这样的问题。

提前致谢。任何回应都非常感谢。

1 个答案:

答案 0 :(得分:1)

检查chatBot.getGreetingsInputs()中的所有值。检查列表中是否有空元素。

例如,在这个问题Android App crashes after Scrolling down ListView中,有2个最后的记录已损坏。