首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:启动对话框关闭失败: java.lang.IllegalArgumentException

错误:启动对话框关闭失败: java.lang.IllegalArgumentException
EN

Stack Overflow用户
提问于 2017-11-27 08:12:25
回答 2查看 707关注 0票数 0

我之前已经问过这个问题了,here,但是没有人能够回答这个问题。问题是,在“创建”的活动中,我创建了一个警报对话框,这个方法在Android5.0上运行得很好,但是在android中,我得到了一个错误:

逻辑猫:

代码语言:javascript
复制
E/HwLauncher: Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog

下面是我的活动代码

代码语言:javascript
复制
  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部分,但是错误仍然存在。下面是我现在使用的没有警报对话框的代码

代码语言:javascript
复制
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也会显示相同的错误:

代码语言:javascript
复制
Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog
EN

回答 2

Stack Overflow用户

发布于 2017-11-27 08:55:54

代码语言:javascript
复制
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();
        }
    }
}

我试过这样做,而且效果很好。下面是我的分级文件

代码语言:javascript
复制
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'
}
票数 0
EN

Stack Overflow用户

发布于 2019-07-04 08:08:37

代码语言:javascript
复制
HwLauncher: Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog

我在HuaWei MT7-L09(mate7)、EMUI3.0、Android为4.4.2中获得了相同的日志。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47506358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档