java - Android Resources$NotFoundException: Resource ID #0x0 -
right trying create activity edittext , button, , below listview can dynamically add strings list. i'm getting errors trying mock activity.
here main activity.
public class mainactivity extends activity { private connection serverconnection; private arraylist<string> listitems = new arraylist<string>(); private arrayadapter<string> adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview listview = (listview) findviewbyid(r.id.list); adapter = new arrayadapter<string>(mainactivity.this, 0, listitems); listview.setadapter(adapter); serverconnection = new connection(mainactivity.this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } public void displaymessage(string string) { listitems.add(string); adapter.notifydatasetchanged(); } } here xml main activity.
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 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" tools:context=".mainactivity" > <edittext android:id="@+id/message_entry" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_alignleft="@+id/list" android:layout_toleftof="@+id/send_button" android:layout_weight="1" android:hint="@string/enter_message" /> <listview android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/message_entry" android:paddingtop="10dp" /> <button android:id="@+id/send_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/message_entry" android:layout_alignright="@+id/list" android:layout_aligntop="@+id/message_entry" /> </relativelayout> here other class i'm going use connecting socket.
public class connection extends thread { private socket client; private objectoutputstream output; private objectinputstream input; public connection(mainactivity mainactivity) { mainactivity.displaymessage("test1"); mainactivity.displaymessage("test2"); mainactivity.displaymessage("test3"); mainactivity.displaymessage("test4"); mainactivity.displaymessage("test5"); } }
this line gives error
adapter = new arrayadapter<string>(mainactivity.this, 0, listitems); the second parameter accepts layout resourceid can populate value (string) layout.
you can use layout of android framework android.r.layout.simple_list_item_1
adapter = new arrayadapter<string>(mainactivity.this, android.r.layout.simple_list_item_1, listitems);
Comments
Post a Comment