Reputation: 161
I have a dialog screen and inside the dialog I have a listview. I want to show which item is clicked from the listview by a toast message. So I tried to display a message of the clicked item by a toast message using listview.getSelectedItem().toString() but it crashed when clicked on the list item. But no crash happen if I simply display a string by toast when an item is clicked in the listview. for eg:
Toast.makeText(MainActivity.this, "Hello World", Toast.LENGTH_LONG).show();
But crashed in the below code:
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): Database already exists 06-02 23:53:35.321: W/KeyCharacterMap(8581): No keyboard for id -1 06-02 23:53:35.321: W/KeyCharacterMap(8581): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 06-02 23:53:41.291: W/dalvikvm(8581): threadid=1: thread exiting with uncaught exception (group=0x2aac8830) 06-02 23:53:41.291: E/AndroidRuntime(8581): FATAL EXCEPTION: main 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): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 06-02 23:53:41.291: E/AndroidRuntime(8581): at android.widget.ListView.performItemClick(ListView.java:3382) 06-02 23:53:41.291: E/AndroidRuntime(8581): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1702) 06-02 23:53:41.291: E/AndroidRuntime(8581): at android.os.Handler.handleCallback(Handler.java:587) 06-02 23:53:41.291: E/AndroidRuntime(8581): at android.os.Handler.dispatchMessage(Handler.java:92) 06-02 23:53:41.291: E/AndroidRuntime(8581): at android.os.Looper.loop(Looper.java:123) 06-02 23:53:41.291: E/AndroidRuntime(8581): at 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)
Upvotes: 0
Views: 169
Reputation: 41
For one thing, check if book_list.getSelectedItem()
is null
.
Next you want to do a book_list.getItemAtPosition(pos)
Upvotes: 1
Reputation: 2834
Normally, when you click an item in a listview, it doesn't immediately change its state to selected. You should get the item in the following way:
book_list.getItemAtPosition(position)
Upvotes: 2