【问题标题】:Error : Launcher dialog dismiss failed : java.lang.IllegalArgumentException错误:启动器对话框关闭失败:java.lang.IllegalArgumentException
【发布时间】: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


【解决方案1】:
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();
        }
    }
}

我试过这样,它正在工作。下面是我的 gradle 文件

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'
}

【讨论】:

  • 我使用的是完全相同的代码,只是我的 minSdk 是 14,因为我也在较低的 API 级别上进行测试。该代码在 API 22 上运行良好,但在较低的代码上,我得到了以上错误
  • 你在用什么设备测试
  • 我用于测试的设备是 API 19
  • 现在我什至尝试注释掉 AlertDialog 部分,但错误仍然相同
【解决方案2】:
HwLauncher: Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog

我在华为MT7-L09(mate7)得到同样的登录,EMUI 3.0,Android版本是4.4.2。

日志不是你的应用程序的,我发现当我打开手机的任务菜单时,这个日志会显示在 logcat 中。

【讨论】:

    猜你喜欢
    • 2015-11-10
    • 2016-06-27
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多