Reputation: 6350
I have a custom dialog .So a button click i am showing it .Now i have a button in the custom dialog i want to close it on click of that button but it is throwing any null pointer exception.Here is my code that i am using to show it:
private void showPreConfirmationDialog() {
final Dialog dialog= new Dialog(context);;
button = (ImageView) findViewById(R.id.bookButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dialog.setContentView(R.layout.pre_confirmation_dailog);
//dialog.setCancelable(false);
dialog.setTitle("OnWard Details...");
dialog.show();
}
});
backPreConfirmation = (ImageView)dialog.findViewById(R.id.backImage);
backPreConfirmation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
}
On click of backPreConfirmation button it is throwing my this error :
07-30 09:25:15.830: E/AndroidRuntime(26599): FATAL EXCEPTION: main
07-30 09:25:15.830: E/AndroidRuntime(26599): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.result/com.android.result.Result}: java.lang.NullPointerException
07-30 09:25:15.830: E/AndroidRuntime(26599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-30 09:25:15.830: E/AndroidRuntime(26599): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-30 09:25:15.830: E/AndroidRuntime(26599): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-30 09:25:15.830: E/AndroidRuntime(26599): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-30 09:25:15.830: E/AndroidRuntime(26599): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 09:25:15.830: E/AndroidRuntime(26599): at android.os.Looper.loop(Looper.java:137)
07-30 09:25:15.830: E/AndroidRuntime(26599): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-30 09:25:15.830: E/AndroidRuntime(26599): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 09:25:15.830: E/AndroidRuntime(26599): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 09:25:15.830: E/AndroidRuntime(26599): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-30 09:25:15.830: E/AndroidRuntime(26599): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-30 09:25:15.830: E/AndroidRuntime(26599): at dalvik.system.NativeStart.main(Native Method)
07-30 09:25:15.830: E/AndroidRuntime(26599): Caused by: java.lang.NullPointerException
07-30 09:25:15.830: E/AndroidRuntime(26599): at com.android.result.Result.showPreConfirmationDialog(Result.java:66)
07-30 09:25:15.830: E/AndroidRuntime(26599): at com.android.result.Result.onCreate(Result.java:41)
07-30 09:25:15.830: E/AndroidRuntime(26599): at android.app.Activity.performCreate(Activity.java:5104)
07-30 09:25:15.830: E/AndroidRuntime(26599): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-30 09:25:15.830: E/AndroidRuntime(26599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-30 09:25:15.830: E/AndroidRuntime(26599): ... 11 more
What i have wrong please let me know
Upvotes: 6
Views: 17554
Reputation: 2066
the solution above works but it is a bit long, I guess the best solution is to make the Dialog object global or field of the activity you are working at, then when you finish working with it, use the method dismiss() on the object.
//out side any method(global domain)
private Dialog dialog;
//inside the button handler of the dialog
dialog.dismiss();
Upvotes: 0
Reputation: 1
try this. ...
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final Dialog dialog = builder.create();
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
dialog.dismiss();
}
});
dialog.show();
...
Upvotes: 0
Reputation: 133560
You need to use the dialog object to initialize your views
final Dialog dialog= new Dialog(context);
dialog.setContentView(R.layout.pre_confirmation_dailog);
backPreConfirmation = (ImageView)dialog.findViewById(R.id.backImage);
Initialize this in onCreate
button = (ImageView) findViewById(R.id.bookButton);
Then
button.setOnClickListener( new OnClickListener()
{
public void onClick(View v)
{
showPreConfirmationDialog();
}
});
In showPreConfirmationDialog()
private void showPreConfirmationDialog() {
final Dialog dialog= new Dialog(context);
dialog.setContentView(R.layout.pre_confirmation_dailog);
dialog.setTitle("Loading...");
dialog.show();
backPreConfirmation = (ImageView)dialog.findViewById(R.id.backImage);
backPreConfirmation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
}
Note : You can findViewById
of the current view hierarchy set to the activity. You set the content of your layout to dialog. And you have button backPreConfirmation
in that layout. So you need to use the dialog object to inflate your button.
Upvotes: 2
Reputation: 800
private void showPreConfirmationDialog() {
button = (ImageView) findViewById(R.id.bookButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final Dialog dialog= new Dialog(context);
dialog.setContentView(R.layout.custom);// Custome layout xml
dialog.setTitle("OnWard Details...");
backPreConfirmation = (ImageView)dialog.findViewById(R.id.backImage);
backPreConfirmation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
try this
Upvotes: 0
Reputation: 2386
You are setting reference of current activity view to dialog's child, instead of this use dialog for setting up reference to all dialog child button = (ImageView)dialog.findViewById(R.id.bookButton);
backPreConfirmation = (ImageView)dialog.findViewById(R.id.backImage);
Upvotes: 0
Reputation: 2059
do this
button = (ImageView) dialog.findViewById(R.id.bookButton);
this might solve your problem.
Upvotes: 0
Reputation: 8251
Its because "backPreConfirmation" is not inside your main layout.It is inside your "pre_confirmation_dailog
" layout. To get this method do the following,
replace
backPreConfirmation = (ImageView)findViewById(R.id.backImage);
with
backPreConfirmation = (ImageView)dialog.findViewById(R.id.backImage);
Upvotes: 0
Reputation: 2104
you have to change for both like this:
button = (ImageView) dialog.findViewById(R.id.bookButton);
backPreConfirmation = (ImageView)dialog.findViewById(R.id.backImage);
So,now you can get proper solution.
Upvotes: 0