2013-07-30 49 views
2

我有一个自定义对话框。所以一个按钮单击我显示它。现在我有一个按钮在自定义对话框中我想关闭它按一下该按钮,但它是抛出任何空指针exception.Here是我的代码,我使用,以显示它:在Android中点击按钮关闭一个自定义对话框

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(); 

     } 
    }); 
} 

在backPreConfirmation按钮,点击它抛出我的这个错误:

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 

我有什么错了,请让我知道

回答

2

您需要使用对话框对象初始化你的意见

final Dialog dialog= new Dialog(context); 
    dialog.setContentView(R.layout.pre_confirmation_dailog); 
    backPreConfirmation = (ImageView)dialog.findViewById(R.id.backImage); 

onCreate

button = (ImageView) findViewById(R.id.bookButton); 

然后

button.setOnClickListener(new OnClickListener() 
{ 
    public void onClick(View v) 
    { 
     showPreConfirmationDialog(); 
    } 
}); 

初始化这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(); 

    } 
}); 
} 

注意:您可以将当前视图层次结构的findViewById设置为该活动。您将布局的内容设置为对话框。并且您在该布局中有按钮backPreConfirmation。所以你需要使用对话框来夸大你的按钮。

+0

我做了同样的变化还是同样的问题.same错误在错误日志 – Developer

+0

移动'dialog.show()'按钮点击收听外面 – Raghunandan

+0

按钮在主布局和backPreConfirmation在preconformation对话框布局 – Developer

0

你有两个这样的改变:

button = (ImageView) dialog.findViewById(R.id.bookButton); 
backPreConfirmation = (ImageView)dialog.findViewById(R.id.backImage); 

所以,现在你可以得到妥善解决。

0

这是因为“backPreConfirmation”不在您的主布局中。它位于您的“pre_confirmation_dailog”布局中。为了得到这个方法做到以下几点,

replace 
    backPreConfirmation = (ImageView)findViewById(R.id.backImage); 
with 

    backPreConfirmation = (ImageView)dialog.findViewById(R.id.backImage); 
0

做到这一点

button = (ImageView) dialog.findViewById(R.id.bookButton); 

这可能会解决你的问题。

0

您参考设置的所有对话框孩子button = (ImageView)dialog.findViewById(R.id.bookButton); backPreConfirmation = (ImageView)dialog.findViewById(R.id.backImage);

0

私人无效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(); 

     } 
    }); 

} 

尝试设置当前活动视图的参考对话框的孩子,而不是这种使用对话框这

+1

非常感谢您的帮助 – Developer

+0

Dose it work ... –

0

试试这个。 ...

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(); 

...

+2

尝试添加更多关于您为什么用这种方式回答的描述。请阅读回答问题的指导原则:https://stackoverflow.com/help/how-to-answer – TrevorBrooks

+0

欢迎来到Stack Overflow!尽管这段代码可以解决这个问题,但[包括一个解释](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)确实有助于提高您的帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性注释来挤占代码,这会降低代码和解释的可读性! – Machavity

0

上述工程的解决方案,但它是一个有点长,我想最好的解决方案是让对话框对象全局或场你在工作活动,那么当你完成它的工作时,使用对象的方法dismiss()。

//out side any method(global domain) 
private Dialog dialog; 

//inside the button handler of the dialog 
dialog.dismiss(); 
相关问题