我创建了一个对话框片段。当我按下子菜单项时,我需要让这个对话框进入我当前的活动。所以我创建并声明了AlertDialog.Builder构建;在我的活动开始之内。
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
答案 0 :(得分:1)
首先,变量builder
未初始化,因为onCreateDialog()
未被调用...
您可能认为自己正在覆盖onCreateDialog()
的{{1}}方法,但实际上并非如此。
查看Activity
onCreateDialog()
的定义
Activity
仅供参考...这两种方法已被弃用。
但你写了
protected Dialog onCreateDialog(int);
protected Dialog onCreateDialog(int,Bundle);
不会覆盖protected Dialog onCreateDialog(Bundle);
的任何方法...(使用Activity
注释来了解您实际上是否覆盖)
您可以在不使用Override
方法时删除Bundle
参数。
onCreateDialog()
并在点击适当的public Dialog onCreateDialog() {
// 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();
}
时调用此方法...
MenuItem