2012-08-05 74 views
0

我已经读过这里和其他网站的冲突报告,说它是/不可能显示来自其他活动的警报对话框。我试图做到这一点的:Android open另一个活动的AlertDialog

public class Options extends Activity { 

    /** Include classes */ 
    SharedPreferences sharedPrefs; 
    Preferences prefs; 
    Location loc; 
    LocationSQL locSQL; 
    NetworkConnection netConnect; 

    /** Declare buttons */ 
    Button bLocation; 
    Button bRefresh; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.options); 

     sharedPrefs = PreferenceManager.getDefaultSharedPreferences(Options.this); 

     /** Set the location for the user */ 
     setLocationPref(); 

     /** Refresh selection */ 
     bRefresh = (Button) findViewById(R.id.bRefresh); 
     bRefresh.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
     loc = new Location(getApplicationContext()); 
      try{ 

       loc.locationSelection(); 
      }catch (Exception e){ 
       Log.d("ERROR", "Catch " + e); 
      } 
      } 
     }); 

     /** Location selection */ 
     bLocation = (Button) findViewById(R.id.bLocation); 
     bLocation.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

      } 
     }); 
    } 
} 

然后,我希望做一些处理,如果需要,可以调用一个AlertDialog框。但是我得到下面的错误。我知道如果我是正确的,我正在使用已被销毁的上下文?但我想不出如何解决这个问题?

public class Location { 

    private static final String Context = null; 
    NetworkConnection nc; 
    SharedPreferences getSharedPrefs; 
    SharedPreferences putSharedPrefs; 
    NetworkConnection netConnect; 
    Options opts; 
    Context context; 

    public Location (Context arg) 
    { 
     context = arg; 
    } 

    public void locationSelection(){ 

     message(); 

    } 

    public void message(){ 

     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 
     alertDialogBuilder.setTitle("Your Title"); 
     AlertDialog alertDialog = alertDialogBuilder.create(); 
     alertDialog.show(); 

    } 

} 

Catch android.view.WindowManager $ BadTokenException:无法添加窗口 - 标记null不适用于应用程序。

非常感谢您的帮助:-)

回答

0

不是传递getApplicationContext()到您的位置构造尝试通过YourClass.this的(所以在这里,Options.this)。

此外,这行代码是什么?

private static final String Context = null; 
+0

感谢yea,似乎有帮助,认为我曾试过:/该行代码是复制和粘贴错误,它不应该在那里,谢谢你的帮助! – James 2012-08-05 12:40:54

+0

确实很高兴它的工作! – DalvikDroid 2012-08-05 12:44:35

相关问题