1

我正在编写一个允许用户登录的应用程序,但是每当我单击登录按钮时,不幸的是应用程序已停止,错误:

            java.lang.NullPointerException

造成的:

DetailActivity,行号:127

   btnDisplay.setOnClickListener(new OnClickListener() 

DetailActivity,行号:62

   showInfo();

Logcat 说::

04-12 10:52:06.250: E/AndroidRuntime(991): FATAL EXCEPTION: main
04-12 10:52:06.250: E/AndroidRuntime(991): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smart.restaurant.versionoct/com.smart.restaurant.versionoct.cart.DetailActivity}: java.lang.NullPointerException
04-12 10:52:06.250: E/AndroidRuntime(991):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-12 10:52:06.250: E/AndroidRuntime(991):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-12 10:52:06.250: E/AndroidRuntime(991):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-12 10:52:06.250: E/AndroidRuntime(991):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-12 10:52:06.250: E/AndroidRuntime(991):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 10:52:06.250: E/AndroidRuntime(991):  at android.os.Looper.loop(Looper.java:137)
04-12 10:52:06.250: E/AndroidRuntime(991):  at android.app.ActivityThread.main(ActivityThread.java:5039)
04-12 10:52:06.250: E/AndroidRuntime(991):  at java.lang.reflect.Method.invokeNative(Native Method)
04-12 10:52:06.250: E/AndroidRuntime(991):  at java.lang.reflect.Method.invoke(Method.java:511)
04-12 10:52:06.250: E/AndroidRuntime(991):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-12 10:52:06.250: E/AndroidRuntime(991):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-12 10:52:06.250: E/AndroidRuntime(991):  at dalvik.system.NativeStart.main(Native Method)
04-12 10:52:06.250: E/AndroidRuntime(991): Caused by: java.lang.NullPointerException
04-12 10:52:06.250: E/AndroidRuntime(991):  at com.smart.restaurant.versionoct.cart.DetailActivity.showInfo(DetailActivity.java:127)
04-12 10:52:06.250: E/AndroidRuntime(991):  at com.smart.restaurant.versionoct.cart.DetailActivity.onCreate(DetailActivity.java:62)
04-12 10:52:06.250: E/AndroidRuntime(991):  at android.app.Activity.performCreate(Activity.java:5104)
04-12 10:52:06.250: E/AndroidRuntime(991):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-12 10:52:06.250: E/AndroidRuntime(991):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-12 10:52:06.250: E/AndroidRuntime(991):  ... 11 more

登录活动.java:

  // btnLogin
        final Button btnLogin = (Button) findViewById(R.id.btnLogin);
        // Perform action on click
        btnLogin.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                String url = "http://ipaddress/checkLogin.php";

        });
    }

DetailActivity.java:

      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkout_member);





    btnDisplay = (ImageButton) findViewById(R.id.buttonSubmit);

    }
    });
}

活动细节.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <include
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    layout="@layout/header_checkout" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="46dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="$"
        android:layout_marginBottom="20dp"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceLarge" />



  <RadioGroup
      android:id="@+id/radioPayMode"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/radioUser"
      android:layout_alignParentRight="true"
      android:layout_below="@+id/textView1"
      android:layout_toRightOf="@+id/radioUser"
      android:background="@color/test4"
      android:layout_marginTop="20dp"
      android:orientation="horizontal" >

      <RadioButton
          android:id="@+id/radioCashOnDelivery"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="8dp"
          android:checked="true"
          android:textStyle="italic"
          android:text="Cash On Delivery"
          android:layout_marginRight="2dp" />

      <RadioButton
          android:id="@+id/radioCreditCard"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_marginTop="8dp"
          android:textStyle="italic"
          android:text="Credit Card" />

  </RadioGroup>

  <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/header"
      android:layout_marginTop="10dp"
      android:text="Amount to Pay:"
      android:layout_marginLeft="10dp"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:textStyle="italic" />


  <TextView
      android:id="@+id/total"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@+id/radioPayMode"
      android:layout_toRightOf="@+id/textView2"
      android:text="0"
      android:layout_marginLeft="2dp"
      android:textAppearance="?android:attr/textAppearanceLarge" />

  <ImageButton
      android:id="@+id/btnSubmit"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_alignRight="@+id/textView2"
      android:layout_marginBottom="22dp"
      android:background="@drawable/place_button" />

  <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/radioPayMode"
      android:text="Name"
      android:layout_marginTop="10dp"
      android:textStyle="bold"
      android:layout_marginLeft="5dp"
      android:textAppearance="?android:attr/textAppearanceMedium" />

  <TextView
      android:id="@+id/textView4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/txtName"
      android:layout_marginTop="10dp"
      android:textStyle="bold"
      android:layout_marginLeft="5dp"
      android:text="Email ID"
      android:textAppearance="?android:attr/textAppearanceMedium" />

  <TextView
      android:id="@+id/txtName"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/textView3"
      android:hint="Your Name Here"
      android:maxLines="1"
      android:layout_marginTop="1dp"
      android:layout_marginLeft="10dp"
      android:textAppearance="?android:attr/textAppearanceSmall"  />

  <TextView
      android:id="@+id/txtEmail"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_centerVertical="true"
      android:hint="Your Email Here"
      android:layout_below="@+id/textView4"
      android:maxLines="1"
      android:layout_marginTop="1dp"
      android:layout_marginLeft="10dp"
      android:textAppearance="?android:attr/textAppearanceSmall"/>

  <TextView
      android:id="@+id/textView5"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView4"
      android:layout_below="@+id/txtEmail"
      android:layout_marginTop="10dp"
      android:textStyle="bold"
      android:text="Contact Number"
      android:textAppearance="?android:attr/textAppearanceMedium" />

  <TextView
      android:id="@+id/txtTel"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_below="@+id/textView5"
      android:hint="Your Mobile Number"
      android:layout_marginTop="1dp"
      android:layout_marginLeft="10dp"
      android:maxLines="1"
      android:textAppearance="?android:attr/textAppearanceSmall" />

  <TextView
      android:id="@+id/textView6"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView5"
      android:layout_below="@+id/txtTel"
      android:layout_marginTop="10dp"
      android:text="Shipping Address"
      android:textStyle="bold"
      android:textAppearance="?android:attr/textAppearanceMedium" />

  <TextView
      android:id="@+id/txtAddress"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/textView6"
      android:gravity="top"
      android:hint="Your Address Here"
      android:inputType="textMultiLine"
      android:layout_marginTop="1dp"
      android:maxLines="3"
      android:layout_marginRight="10dp"
      android:layout_marginLeft="10dp"
      android:textAppearance="?android:attr/textAppearanceSmall" />

 </RelativeLayout>
4

2 回答 2

5

R.id.buttonSubmit转到R.id.btnSubmit第 127 行,您可以在其中按 ID 搜索按钮。您在 XML 中:

  android:id="@+id/btnSubmit"

您的程序正在寻找buttonSubmitin findViewById,但您已将其命名为btnSubmit。因此,您的 ImageButton 变为 null,并调用 NullPointerException。

于 2013-04-12T05:43:29.133 回答
1

您没有在 activity_detail.xml 上声明R.id.buttonSubmit更改 xml 布局,如下所示:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <include
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    layout="@layout/header_checkout" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="46dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="$"
        android:layout_marginBottom="20dp"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceLarge" />



  <RadioGroup
      android:id="@+id/radioPayMode"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/radioUser"
      android:layout_alignParentRight="true"
      android:layout_below="@+id/textView1"
      android:layout_toRightOf="@+id/radioUser"
      android:background="@color/test4"
      android:layout_marginTop="20dp"
      android:orientation="horizontal" >

      <RadioButton
          android:id="@+id/radioCashOnDelivery"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="8dp"
          android:checked="true"
          android:textStyle="italic"
          android:text="Cash On Delivery"
          android:layout_marginRight="2dp" />

      <RadioButton
          android:id="@+id/radioCreditCard"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_marginTop="8dp"
          android:textStyle="italic"
          android:text="Credit Card" />

  </RadioGroup>

  <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/header"
      android:layout_marginTop="10dp"
      android:text="Amount to Pay:"
      android:layout_marginLeft="10dp"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:textStyle="italic" />


  <TextView
      android:id="@+id/total"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@+id/radioPayMode"
      android:layout_toRightOf="@+id/textView2"
      android:text="0"
      android:layout_marginLeft="2dp"
      android:textAppearance="?android:attr/textAppearanceLarge" />

  <ImageButton
      android:id="@+id/buttonSubmit"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_alignRight="@+id/textView2"
      android:layout_marginBottom="22dp"
      android:background="@drawable/place_button" />

  <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/radioPayMode"
      android:text="Name"
      android:layout_marginTop="10dp"
      android:textStyle="bold"
      android:layout_marginLeft="5dp"
      android:textAppearance="?android:attr/textAppearanceMedium" />

  <TextView
      android:id="@+id/textView4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/txtName"
      android:layout_marginTop="10dp"
      android:textStyle="bold"
      android:layout_marginLeft="5dp"
      android:text="Email ID"
      android:textAppearance="?android:attr/textAppearanceMedium" />

  <TextView
      android:id="@+id/txtName"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/textView3"
      android:hint="Your Name Here"
      android:maxLines="1"
      android:layout_marginTop="1dp"
      android:layout_marginLeft="10dp"
      android:textAppearance="?android:attr/textAppearanceSmall"  />

  <TextView
      android:id="@+id/txtEmail"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_centerVertical="true"
      android:hint="Your Email Here"
      android:layout_below="@+id/textView4"
      android:maxLines="1"
      android:layout_marginTop="1dp"
      android:layout_marginLeft="10dp"
      android:textAppearance="?android:attr/textAppearanceSmall"/>

  <TextView
      android:id="@+id/textView5"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView4"
      android:layout_below="@+id/txtEmail"
      android:layout_marginTop="10dp"
      android:textStyle="bold"
      android:text="Contact Number"
      android:textAppearance="?android:attr/textAppearanceMedium" />

  <TextView
      android:id="@+id/txtTel"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_below="@+id/textView5"
      android:hint="Your Mobile Number"
      android:layout_marginTop="1dp"
      android:layout_marginLeft="10dp"
      android:maxLines="1"
      android:textAppearance="?android:attr/textAppearanceSmall" />

  <TextView
      android:id="@+id/textView6"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView5"
      android:layout_below="@+id/txtTel"
      android:layout_marginTop="10dp"
      android:text="Shipping Address"
      android:textStyle="bold"
      android:textAppearance="?android:attr/textAppearanceMedium" />

  <TextView
      android:id="@+id/txtAddress"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/textView6"
      android:gravity="top"
      android:hint="Your Address Here"
      android:inputType="textMultiLine"
      android:layout_marginTop="1dp"
      android:maxLines="3"
      android:layout_marginRight="10dp"
      android:layout_marginLeft="10dp"
      android:textAppearance="?android:attr/textAppearanceSmall" />

 </RelativeLayout>
于 2013-04-12T05:45:57.337 回答