0

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
usama ahsan
  • 43
  • 1
  • 6

2 Answers2

0
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private static int SPLASH_TIME_OUT = 3000;

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

        // Alert Box
        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) {
                        //dialog.dismiss();
                        finish();
                        startActivity(new Intent(Settings.ACTION_SETTINGS));
                    }
                });

        builder.setNegativeButton(
                "Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        finish();
                    }
                });
        Dialog alert = builder.create();

        ConnectivityManager cm = (ConnectivityManager)
                getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo networkInfo = cm.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // close this activity
                        finish();
                        // This method will be executed once the timer is over
                        // Start your app main activity
                        Intent i = new Intent(MainActivity.this, MainActivity.class);
                        startActivity(i);
                    }
                }, SPLASH_TIME_OUT);
            } else {
                alert.show();
            }
        } else {
            alert.show();
        }
    }
}

i tried like this and it is working. below is my gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.manoj.demoapplication"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Manoj kumar
  • 450
  • 4
  • 13
0
HwLauncher: Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog

i got the same log in HuaWei MT7-L09(mate7), EMUI 3.0 , Android vesion is 4.4.2 .

and the log is not your app's, i find when i open the phone's task menu, this log will show in the logcat.

103style
  • 61
  • 1
  • 5