0

Ich habe diese Frage früher gestellt here, aber niemand war in der Lage, diese auch zu beantworten. Das Problem ist, dass in meiner Tätigkeit onCreate ich einen Alarm Dialog erschaffe und diese Methode läuft perfekt auf Android 5.0, aber in Android API weniger als 5 Ich erhalte eine Fehlermeldung:Fehler: Launcherdialogabweisung ist fehlgeschlagen: java.lang.IllegalArgumentException

Logcat:

E/HwLauncher: Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog 

unten ist mein Code für die Aktivität

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); 

     } 
    } 

ich habe versucht, andere Antworten von Stackoverflow like this one aber keine dieser Antworten half mir

EDIT:

Ich habe den AlertDialog-Teil des Codes auskommentiert, aber der Fehler besteht immer noch. Unten ist der Code, den ich jetzt ohne bin mit 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); 

     }*/ 
    } 

Auch mit dem Alertdialog aus Logcat kommentiert zeigt den gleichen Fehler:

Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog 
+0

insted AlertDilaog der Verwendung versuchen, nur letzten Dialog alert = builder.create Verwendung(); –

+0

https://StackOverflow.com/Questions/5815089/android-illegalargumentException-for-Dismissdialog –

+0

@Manojkumar Ich versuchte diese Lösung, aber der Fehler ist immer noch der gleiche –

Antwort

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(); 
     } 
    } 
} 

ich so versucht und es funktioniert. unten ist meine gradle Datei

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

Ich benutze den genau gleichen Code mit Ausnahme der MinSdk in meinem Fall ist 14, weil ich auf niedrigeren API-Ebenen zu testen bin. Der Code funktioniert perfekt auf API 22, aber auf niedrigeren, bekomme ich über Fehler –

+0

welches Gerät verwenden Sie Test –

+0

nach dem Wechsel zu 14 es funktioniert gut kein Fehler –

Verwandte Themen