来自oncreate的警告对话框

时间:2014-07-02 09:51:32

标签: android android-alertdialog splash-screen

我在onCreate中启动了一个警告对话框,允许用户从两个不同的活动中进行选择。我使用了具有正值和负值的警告对话框但不幸的是我收到了错误。是否无法使用此类警报对话框来运行两种不同的活动?如果是的话怎么可能? 我遇到这种错误:

[Android Application]   
[Android Application]   
[Android Application]   
[Android Application]   
[Android Application]   
[Android Application]   
[Android Application]   
[Android Application]   
    DalvikVM [localhost:8602]   
        Thread [<1> main] (Running) 
        Thread [<10> Binder_2] (Running)    
        Thread [<9> Binder_1] (Running) 
        Thread [<11> Thread-198] (Suspended (exception RuntimeException))   
            <VM does not provide monitor information>   
            Handler.<init>(Handler$Callback, boolean) line: 200 
            Handler.<init>() line: 114  
            AlertDialog(Dialog).<init>(Context, int, boolean) line: 109 
            AlertDialog.<init>(Context, int, boolean) line: 114 
            AlertDialog$Builder.create() line: 931  
            MainActivity$splashscreen.run() line: 68    
[Android Application]   

这是警报对话框的代码

public class MainActivity extends Activity {

    //ImageView image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Removes title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);    
        // Removes notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);    
        setContentView(R.layout.activity_main);
        splashscreen view = new splashscreen();
        view.start();

    }

    public class splashscreen extends Thread{
        public void run(){
            try{
                Thread.sleep(3*1000);
            }catch(Exception  e){
                Log.v("Exception", e.toString());
            }
    AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);

                builder.setTitle("Choose your activity to start");

                builder.setMessage("Pick desired activity");

                builder.setPositiveButton("Office",new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent home = new Intent(getApplicationContext(), OfficeActivity.class);
                        startActivity(home);
                        finish();
                        //dialog.cancel();
                    }
                });
                builder.setNegativeButton("School", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent home = new Intent(getApplicationContext(), SchoolActivity.class);
                        startActivity(home);
                        finish();
                        //dialog.cancel();
                    }
                });
                AlertDialog alertdialog=builder.create();
                alertdialog.show();
        }
    }
}

2 个答案:

答案 0 :(得分:7)

这是您问题的解决方案,试试这个......

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Choose your activity to start");
            builder.setMessage("Pick desired activity");

            builder.setPositiveButton("Office",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    Intent home = new Intent(getApplicationContext(), OfficeActivity.class);
                    startActivity(home);
                    finish();
                }
            });

            builder.setNegativeButton("School", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    Intent home = new Intent(getApplicationContext(), SchoolActivity.class);
                    startActivity(home);
                    finish();
                }
            });
            AlertDialog alertdialog=builder.create();
            alertdialog.show();
        }
    }, 4000);
}

答案 1 :(得分:2)

要显示您的 AlertDialog ,请在用户界面线程

中运行您的代码

试试这个..

Activity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
       // Your code to run in GUI thread here
    }
});