2013-04-12 252 views
1

我寫一個應用程序中對此我允許用戶進行登陸,但每當我做點擊登錄按鈕讓不幸的是應用程序已經停止,錯誤:顯示java.lang.NullPointerException - 不幸的是應用程序已經停止

  java.lang.NullPointerException 

造成的:

DetailActivity,行號:

btnDisplay.setOnClickListener(new OnClickListener() 

DetailActivity,行號:

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 

LoginActivity.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); 

    } 
    }); 
} 

activity_detail.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> 
+1

什麼是線127 showInfo()? – Zyerah

+1

發佈您的詳細信息活動佈局xml – Dinesh

+0

@Telthien我已經將該方法代碼放在DetailActivity.java中 – ASMUIRTI

回答

5

變化R.id.buttonSubmitR.id.btnSubmit上線127,在這裏您可以通過ID搜索按鈕。你必須在XML:

android:id="@+id/btnSubmit" 

你的程序正在尋找在findViewByIdbuttonSubmit,但你把它命名爲btnSubmit。因此,您的ImageButton變爲null,並調用NullPointerException。

1

你不喜歡以下聲明R.id.buttonSubmit上activity_detail.xml改變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> 
相關問題