【发布时间】:2019-07-04 16:08:37
【问题描述】:
我之前已经问过这个问题here,但也没有人能够回答这个问题。问题是,在我的活动 oncreate 中,我正在创建一个警报对话框,并且此方法在 android 5.0 上完美运行,但在 android API 小于 5 时出现错误:
Logcat:
E/HwLauncher: Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog
下面是我的活动代码
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// Alert Box
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Internet not connected");
builder.setMessage("You need to connect WiFi/Mobile-Data run this app.");
builder.setCancelable(false);
builder.setPositiveButton(
"Go to Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(Settings.ACTION_SETTINGS));
finish();
}
});
builder.setNegativeButton(
"Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
final AlertDialog alert = builder.create();
alert.show();
ConnectivityManager cm = (ConnectivityManager)
getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null){
alert.dismiss();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(splash.this, LoginActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
我尝试过 stackoverflow like this one 的其他答案,但这些答案都没有帮助我
编辑:
我已注释掉代码的 AlertDialog 部分,但错误仍然存在。下面是我现在使用的没有 alertdialog 的代码
public class splash extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(splash.this, LoginActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
// Alert Box
/*
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Internet not connected");
builder.setMessage("You need to connect WiFi/Mobile-Data run this app.");
builder.setCancelable(false);
builder.setPositiveButton(
"Go to Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(Settings.ACTION_SETTINGS));
finish();
}
});
builder.setNegativeButton(
"Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
final Dialog alert = builder.create();
alert.show();
//WifiManager wifi =(WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
ConnectivityManager cm = (ConnectivityManager)
getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null){
alert.dismiss();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(splash.this, LoginActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}*/
}
即使将 AlertDialog 注释掉,Logcat 也会显示相同的错误:
Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog
【问题讨论】:
-
不再使用 AlertDilaog 尝试使用 final Dialog alert = builder.create();
-
@Manojkumar 我试过这个解决方案,但错误还是一样
-
@IntelliJAmiya 在此线程中标记为正确的 removedialog(int i) 方法现在已被 API 13 弃用
-
当我尝试它时它工作正常
标签: java android android-alertdialog illegalargumentexception