Android 来自oncreate的警报对话框
我在onCreate中启动了一个警报对话框,允许用户从两个不同的活动中进行选择。我使用了警报对话框,该对话框有正值和负值,但不幸的是,我得到了一个错误。是否不能使用此类警报对话框来运行两个不同的活动?如果是,怎么可能? 我得到这样的错误:Android 来自oncreate的警报对话框,android,android-alertdialog,splash-screen,Android,Android Alertdialog,Splash Screen,我在onCreate中启动了一个警报对话框,允许用户从两个不同的活动中进行选择。我使用了警报对话框,该对话框有正值和负值,但不幸的是,我得到了一个错误。是否不能使用此类警报对话框来运行两个不同的活动?如果是,怎么可能? 我得到这样的错误: [Android Application] [Android Application] [Android Application] [Android Application] [Android Application] [Andr
[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]
要显示AlertDialog,请在UI线程中运行代码
试试这个
Activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// Your code to run in GUI thread here
}
});
这是你问题的解决方案,试试这个
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);
}
这是可能的。相信我。你应该避免的唯一一件事是在与ui线程不同的线程中触摸ui元素。。。真正的错误是什么?我已经找了好几个小时了,我不知道我在这里做错了什么..仍然在寻找答案哪一行给了你错误?@blackbelt你说避免在不同的线程中接触UI元素是什么意思?我不明白你的答案,请解释更多我应该把它放在哪里?我应该放在这里什么?它起作用了很好,先生,但我想在这里实现的是在飞溅警报对话框弹出后,允许用户选择启动哪个活动。有什么想法吗it@Hak哈克,你试过这个密码吗???它显示了一个弹出对话框后,飞溅。。。如果你想了解更多细节,请多解释一下你的问题。但我想发生的是,当飞溅结束时,对话框将显示关于这件事的任何想法?@Hak-Hak当我测试我的代码时,我得到了完美的输出,你在我的代码中更改了什么吗??创建新的projct并将我的代码粘贴到那里,如果它不起作用,让我知道..不,我没有在设备上运行它,因为我在emulator上运行它,可能这就是它有延迟的原因..我的意思是,如果我选择任何按钮,在显示所选活动之前会有一点延迟。但是代码真的很完美
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);
}