I have asked this question earlier here but no one was able to answer that one as well. The problem is that in my activity oncreate i am creating a alert dialog and this method runs perfectly on android 5.0 but in android API less than 5 I get an error:
Logcat:
E/HwLauncher: Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog
below is my code for activity
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);
}
}
I have tried other answers from stackoverflow like this one but none of these answers helped me
EDIT:
I have commented out the AlertDialog part of the code but the error still persists. Below is the code I am using now without 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);
}*/
}
Even with the AlertDialog commented out Logcat shows the same error:
Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog