Why does my Android Application crash after I add a listView to it? -
it ran correctly before added new code program. wanted add listview show message, doesn't work. when app gets activity, program crashes... added :
string[] studentlist = {"zhangsan","lisi"}; listview stdlistview = (listview)findviewbyid(r.id.studentlistview); arrayadapter<string> arrayadapter; arrayadapter = new arrayadapter<string>(this,android.r.layout.simple_list_item_2,studentlist); stdlistview.setadapter(arrayadapter); stdlistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string result = parent.getitematposition(position).tostring(); alertdialog.builder choice = new alertdialog.builder(editactivity.this); choice.settitle(result); } });
to:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_edit); string[] studentlist = {"zhangsan","lisi"}; listview stdlistview = (listview)findviewbyid(r.id.studentlistview); arrayadapter<string> arrayadapter; arrayadapter = new arrayadapter<string>(this,android.r.layout.simple_list_item_2,studentlist); stdlistview.setadapter(arrayadapter); stdlistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string result = parent.getitematposition(position).tostring(); alertdialog.builder choice = new alertdialog.builder(editactivity.this); choice.settitle(result); } }); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } }); drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle toggle = new actionbardrawertoggle( this, drawer, r.string.navigation_drawer_open, r.string.navigation_drawer_close); drawer.setdrawerlistener(toggle); toggle.syncstate(); navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this); }
and following xml file of layout. , added:
<listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/studentlistview" android:layout_alignparentright="true" android:layout_alignparentend="true" />
to:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="n402lab.door.editactivity" tools:showin="@layout/app_bar_edit"> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/studentlistview" android:layout_alignparentright="true" android:layout_alignparentend="true" /> </relativelayout>
after saw comment on question, tried debug myself. before didn't know how debug application. saw "you must supply resource id textview" , search solution , replace "android.r.layout.simple_list_item_2" "android.r.layout.simple_list_item_1" , works.! much!
Comments
Post a Comment