2011-05-18 32 views
0

我正在寻找一种显示对话框的方式,无论显示何种活动。如何从应用程序实例中触发对话框?

我想什么,我想要实现更好的使用一些代码解释:

public class MyApp extends Application { 

     public MyApplication() { 
     } 

     @Override 
     public void onCreate() { 
       super.onCreate(); 

       boolean success = doSomeWebServiceCall(); 

       if (!success) 
         showAlertDialog(); // fails with error 
     } 
} 

我得到的错误是:

ERROR/AndroidRuntime(375): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
at android.view.ViewRoot.setView(ViewRoot.java:460) 

回答

2

只有一个Activity可以显示Dialog。考虑使用对话主题Activity来代替,它可以通过startActivity()Application对象启动。

+0

谢谢,我会试一试! – David 2011-05-18 05:02:07

+0

你给startActivity的背景是什么? – David 2011-05-18 17:57:35

+0

@David:'Application'是一个'Context'。 – CommonsWare 2011-05-18 17:58:18

2

我想敬酒(甚至可以扔在如果您不需要用户交互,则更方便

Toast toast = Toast.makeText(getApplicationContext(), "YOUR TEXT HERE", Toast.LENGTH_LONG); 
LinearLayout toastView = (LinearLayout) toast.getView(); 
ImageView infoImage = new ImageView(getApplicationContext()); 
infoImage.setImageResource(drawable.your_image); 
toastView.addView(infoImage, 0); 
toast.show(); 
+0

这很有趣,看到你可以用烤面包做到这一点!但我想确保用户看到对话/祝酒。有没有办法阻止吐司自动消失? – David 2011-05-18 05:07:08

+0

@David:不,对不起,“Toast”必须自动消失,因为它通常不会响应用户输入。 – CommonsWare 2011-05-18 10:53:57

相关问题