java - Dynamically auto-growing ListView in two layouts -
i have application enter digit in edittext element , when push "add" button, digit copied listview
i can when listview in same activity button , listview
but when change listview activity app crashes , logcat:
java.lang.runtimeexception: unable start activity componentinfo{shop.ned.shahbaa/shop.ned.shahbaa.item1}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) @ android.app.activitythread.access$800(activitythread.java:135) @ android.app.activitythread$h.handlemessage(activitythread.java:1196) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5017) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ shop.ned.shahbaa.item1.oncreate(item1.java:38) @ android.app.activity.performcreate(activity.java:5231) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) at android.app.activitythread.handlelaunchactivity(activitythread.java:2245) at android.app.activitythread.access$800(activitythread.java:135) at android.app.activitythread$h.handlemessage(activitythread.java:1196) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:136) at android.app.activitythread.main(activitythread.java:5017) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:515) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) at com.android.internal.os.zygoteinit.main(zygoteinit.java:595) at dalvik.system.nativestart.main(native method)
how can solve please?
the xml:
<?xml version="1.0" encoding="utf-8"?> <gridlayout xmlns:android="http://schemas.android.com/apk/res/android" android:scrollbaralwaysdrawverticaltrack="true" android:layout_height="fill_parent" android:layout_width="fill_parent"> <textview android:textsize="22sp" android:textcolor="#992222" android:text="@string/label_add_new_product_quantity" android:textdirection="rtl" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_row="0" android:layout_column="0" /> <edittext android:id="@+id/edittextquantityaddproduct" android:hint="@string/add_number" android:layout_width="fill_parent" android:inputtype="number|phone" android:layout_row="1" android:layout_column="0" /> <textview android:textsize="22sp" android:id="@+id/unit_kg" android:textcolor="#992222" android:text="@string/unit_kg" android:textdirection="rtl" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_row="2" android:layout_column="0" /> <view android:layout_width="match_parent" android:layout_height="1.5sp" android:background="#ffcdcc" android:layout_row="3" android:layout_column="0" /> <textview android:textsize="22sp" android:layout_width="fill_parent" android:textcolor="#992222" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/label" android:layout_row="4" android:layout_column="0" /> <textview android:textsize="22sp" android:id="@+id/label_item2" android:layout_width="fill_parent" android:textcolor="#992222" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/label_item2" android:layout_row="5" android:layout_column="0" /> <view android:layout_width="match_parent" android:layout_height="1.5sp" android:background="#ffcdcc" android:layout_row="6" android:layout_column="0" /> <button android:id="@+id/buttonconfirmaddproduct" android:layout_column="0" android:background="#d00000" android:textsize="22sp" android:textcolor="#fff" android:text="@string/button_confirm_adding" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_row="7" android:layout_margintop="10sp"/> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listview" android:layout_row="8" android:layout_column="0" /> </gridlayout>
the class:
package shop.ned.shahbaa; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.listview; import android.widget.textview; import java.util.arraylist; import java.util.list; public class item1 extends actionbaractivity { private arrayadapter<string> adapter; private button add; private listview cart; private arraylist<string> item; private edittext et; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_item1); add = (button)findviewbyid(r.id.buttonconfirmaddproduct); cart = (listview)findviewbyid(r.id.listview); et = (edittext) findviewbyid(r.id.edittextquantityaddproduct); item = new arraylist<string>(); (int i=0;i<2;i++){ item.add("row:" + i); } adapter = new arrayadapter<string>(getapplicationcontext(),android.r.layout.simple_list_item_1, item); cart.setadapter(adapter); add.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { item.add(et.gettext().tostring()); adapter.notifydatasetchanged(); } }); } }
like it's working have listview in activity id (shoppinglist) , when change "cart" id it, app crashes.
Comments
Post a Comment