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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With