是否可以在Application类中创建视图?

时间:2015-03-11 15:21:56

标签: android

我正在为所有启动服务使用Application类。是否可以在应用程序类中创建警报对话框?我创建了一个显示错误的对话框

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

这是我的代码

public class Alertpplication extends Application {
 public void onCreate() {
     if (isWifiConnected()) {
    //do some operations
    }  
    else{
 {AlertDialog alertDialog = new AlertDialog.Builder(AlertApplication.this)
            .create();

        // Setting Dialog Title
        alertDialog.setTitle("Alert");



        // Setting Dialog Message
        alertDialog.setMessage("No Wifi Connection");

        // Setting Icon to Dialog
        // alertDialog.setIcon(R.drawable.connection_error);

        // Setting OK Button

        alertDialog.setButton("Connectivity Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // Write your code here to execute after dialog closed
                        // Toast.makeText(getApplicationContext(),
                        // "You clicked on OK",
                        // Toast.LENGTH_SHORT).show();


                        startActivity(new Intent(
                                Settings.ACTION_WIRELESS_SETTINGS));


                    }
                });
       alertDialog.show();

    }
 }


}

0 个答案:

没有答案