我創建了一個對話框片段。當我按下子菜單項時,我需要讓這個對話框來到我目前的活動。所以我創建並聲明瞭AlertDialog.Builder builde;在我的活動開始之前。在android活動的子菜單項中創建對話框片段時出錯
public class Placed_Product_Details extends Activity {
String usrnme,name,proid;
AlertDialog.Builder builde;
其代碼如下。
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
builde = new AlertDialog.Builder(Placed_Product_Details.this);
LayoutInflater inflater = Placed_Product_Details.this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builde.setView(inflater.inflate(R.layout.updateproductname, null))
// Add action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
});
return builde.create();
}
這是子菜單代碼的一部分。
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.delete:
{
//here i have the code for deleting from the database
break;
}
case R.id.updateproductname:
{
//here I have created a dialog and called it here. The code
//of the dialog is posted above
AlertDialog ab=builde.create();
ab.show();
break;
}
default:
break;
}
return super.onOptionsItemSelected(item);
}
這是我在我的項目的res/layout /文件夾中創建的對話框updateproductname.xml的佈局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginTop="30dp"
android:text="Update Product Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/productnameedittext"
android:layout_width="match_parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="30dp"
android:hint="Enter new product name here"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/productnamebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="30dp"
android:text="Change Product Name" />
</LinearLayout>
當我點擊子菜單時,我的應用程序強制突然關閉。我不知道爲什麼?我將顯示顯示錯誤的logcat。這是因爲我錯誤地創建了對話框片段。有人可以告訴我錯誤在我的應用程序中。
編輯:這是我的錯誤的完整的日誌..
02-17 10:50:23.267: D/AndroidRuntime(687): Shutting down VM
02-17 10:50:23.318: W/dalvikvm(687): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-17 10:50:23.416: E/AndroidRuntime(687): FATAL EXCEPTION: main
02-17 10:50:23.416: E/AndroidRuntime(687): java.lang.NullPointerException
02-17 10:50:23.416: E/AndroidRuntime(687): at com.example.onlineauction.Placed_Product_Details.onOptionsItemSelected(Placed_Product_Details.java:188)
02-17 10:50:23.416: E/AndroidRuntime(687): at android.app.Activity.onMenuItemSelected(Activity.java:2205)
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:748)
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137)
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:873)
02-17 10:50:23.416: E/AndroidRuntime(687): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
02-17 10:50:23.416: E/AndroidRuntime(687): at android.widget.ListView.performItemClick(ListView.java:3513)
02-17 10:50:23.416: E/AndroidRuntime(687): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
02-17 10:50:23.416: E/AndroidRuntime(687): at android.os.Handler.handleCallback(Handler.java:587)
02-17 10:50:23.416: E/AndroidRuntime(687): at android.os.Handler.dispatchMessage(Handler.java:92)
02-17 10:50:23.416: E/AndroidRuntime(687): at android.os.Looper.loop(Looper.java:123)
02-17 10:50:23.416: E/AndroidRuntime(687): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-17 10:50:23.416: E/AndroidRuntime(687): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 10:50:23.416: E/AndroidRuntime(687): at java.lang.reflect.Method.invoke(Method.java:507)
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-17 10:50:23.416: E/AndroidRuntime(687): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-17 10:50:23.416: E/AndroidRuntime(687): at dalvik.system.NativeStart.main(Native Method)
02-17 10:50:27.746: I/Process(687): Sending signal. PID: 687 SIG: 9
第二編輯:這裏是顯示錯誤的第二logcat的。
02-17 12:07:11.577: E/AndroidRuntime(769): FATAL EXCEPTION: main
02-17 12:07:11.577: E/AndroidRuntime(769): java.lang.NullPointerException
02-17 12:07:11.577: E/AndroidRuntime(769): at com.example.onlineauction.Placed_Product_Details$1.onClick(Placed_Product_Details.java:212)
02-17 12:07:11.577: E/AndroidRuntime(769): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
02-17 12:07:11.577: E/AndroidRuntime(769): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 12:07:11.577: E/AndroidRuntime(769): at android.os.Looper.loop(Looper.java:123)
02-17 12:07:11.577: E/AndroidRuntime(769): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-17 12:07:11.577: E/AndroidRuntime(769): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 12:07:11.577: E/AndroidRuntime(769): at java.lang.reflect.Method.invoke(Method.java:507)
02-17 12:07:11.577: E/AndroidRuntime(769): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-17 12:07:11.577: E/AndroidRuntime(769): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-17 12:07:11.577: E/AndroidRuntime(769): at dalvik.system.NativeStart.main(Native Method)
02-17 12:07:15.667: I/Process(769): Sending signal. PID: 769 SIG: 9
你能不能張貼整個日誌,而不是它的一部分。 – user1728071
@ user1728071。整個日誌發佈... – njnjnj