我有一个应用程序,我可以在EditText元素中输入一个数字,当我按下“添加”按钮时,这个数字将被复制到ListView
当ListView与按钮和ListView
在同一个活动中时,我可以这样做但是当我将ListView更改为另一个活动时,应用程序崩溃,这就是logcat:
return "error"
我该如何解决这个问题?
XML:
java.lang.RuntimeException: Unable to start activity ComponentInfo{shop.ned.shahbaa/shop.ned.shahbaa.Item1}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
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)
Caused by: java.lang.NullPointerException
at shop.ned.shahbaa.Item1.onCreate(Item1.java:38)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at 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)
班级:
<?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>
像这样工作但我在另一个带有id(shoppingList)的活动中有另一个listview,当我将“cart”id更改为它时,应用程序崩溃了。