I have created a dialog fragment. I need to get this dialog come to my current activity as soon as I press the submenu item. So I have created and declared AlertDialog.Builder builde; inside the starting of my activity.
public class Placed_Product_Details extends Activity {
String usrnme,name,proid;
AlertDialog.Builder builde;
Its code is below.
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();
}
This is the portion of submenu code.
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);
}
This is the layout of my created dialog updateproductname.xml in the res/layout/ folder of my project.
<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>
When I click on the sub menu my application force closes suddenly. I don't know why? I will show the logcat which shows the error. Is this because I created the dialog fragment wrongly. Can someone show me where the error is in my application.
EDIT: Here is the complete log of my error..
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
2nd EDIT: Here is the second logcat showing the errors.
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
first the variable builder
is not initialized as onCreateDialog()
is not called any where...
You may be thinking that you are overriding onCreateDialog()
method of Activity
but actually not.
see the definition of onCreateDialog()
in Activity
protected Dialog onCreateDialog(int);
protected Dialog onCreateDialog(int,Bundle);
FYI... these two methods are deprecated.
but you wrote
protected Dialog onCreateDialog(Bundle);
which does not override any method of Activity
...(use Override
annotation to know you are actually overriding or not)
You can remove the Bundle
parameter from onCreateDialog()
method as you are not using it.
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();
}
and call this method when appropriate MenuItem
is clicked...
case R.id.updateproductname:{
//here I have created a dialog and called it here. The code
//of the dialog is posted above
onCreateDialog().show();
break;
}