应用程序崩溃在listview.getSelectedItem()(ListView里面的Dialog)Android

时间:2015-06-02 18:19:33

标签: android listview dialog

我有一个对话框屏幕,在对话框中我有一个列表视图。我想通过toast消息显示listview中单击的项目。因此,我尝试使用listview.getSelectedItem()。toString()通过toast消息显示所单击项目的消息,但是当单击列表项时它崩溃了。但是,如果我在listview中单击某个项目时只是通过toast显示一个字符串,则不会发生崩溃。例如:

Toast.makeText(MainActivity.this, "Hello World", Toast.LENGTH_LONG).show();

但是在下面的代码中崩溃了:

private void showDialer()
{
    //DIALOG SCREEN
    final Dialog dialog = new Dialog(ActionModes.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    dialog.setContentView(R.layout.dialer_dialog);

    final ListView book_list = (ListView)dialog.findViewById(R.id.listBooks);
    ArrayAdapter<?> adapter_booklist = ArrayAdapter.createFromResource(
            this, R.array.locations, android.R.layout.simple_spinner_item);
            adapter_booklist.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            book_list.setAdapter(adapter_booklist);  

            book_list.setOnItemClickListener(new OnItemClickListener() 
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id)
                {
                    Toast.makeText(MainActivity.this, book_list.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
                }
            });              
    dialog.show();   
}

logcat的

  

06-02 23:53:34.521:I / class com.actionbarsherlock.sample.demos.ExternalDbOpenHelper(8581):数据库已经存在   06-02 23:53:35.321:W / KeyCharacterMap(8581):没有用于id -1的键盘   06-02 23:53:35.321:W / KeyCharacterMap(8581):使用默认键映射:/system/usr/keychars/qwerty.kcm.bin   06-02 23:53:41.291:W / dalvikvm(8581):threadid = 1:线程退出未捕获异常(组= 0x2aac8830)   06-02 23:53:41.291:E / AndroidRuntime(8581):致命异常:主要   06-02 23:53:41.291:E / AndroidRuntime(8581):java.lang.NullPointerException   06-02 23:53:41.291:E / AndroidRuntime(8581):at com.actionbarsherlock.sample.demos.ActionModes $ 2.onItemClick(ActionModes.java:87)   06-02 23:53:41.291:E / AndroidRuntime(8581):在android.widget.AdapterView.performItemClick(AdapterView.java:284)   06-02 23:53:41.291:E / AndroidRuntime(8581):在android.widget.ListView.performItemClick(ListView.java:3382)   06-02 23:53:41.291:E / AndroidRuntime(8581):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:1702)   06-02 23:53:41.291:E / AndroidRuntime(8581):在android.os.Handler.handleCallback(Handler.java:587)   06-02 23:53:41.291:E / AndroidRuntime(8581):在android.os.Handler.dispatchMessage(Handler.java:92)   06-02 23:53:41.291:E / AndroidRuntime(8581):在android.os.Looper.loop(Looper.java:123)   06-02 23:53:41.291:E / AndroidRuntime(8581):在android.app.ActivityThread.main(ActivityThread.java:4627)   06-02 23:53:41.291:E / AndroidRuntime(8581):at java.lang.reflect.Method.invokeNative(Native Method)   06-02 23:53:41.291:E / AndroidRuntime(8581):at java.lang.reflect.Method.invoke(Method.java:521)   06-02 23:53:41.291:E / AndroidRuntime(8581):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:876)   06-02 23:53:41.291:E / AndroidRuntime(8581):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)   06-02 23:53:41.291:E / AndroidRuntime(8581):at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

通常,当您单击列表视图中的某个项目时,它不会立即将其状态更改为 selected 。您应该按以下方式获取该项目:

book_list.getItemAtPosition(position)

答案 1 :(得分:1)

首先,检查book_list.getSelectedItem()是否为null

接下来你要做一个book_list.getItemAtPosition(pos)