2016-08-12 5 views
-3

Dies wurde so oft gefragt, und wurde beantwortet, ich habe dieses Problem zuvor gelöst, aber dieses Mal stecken, Dies funktioniert an einem Ort, aber in einer anderen Aktivität gibt es folgende Fehlermeldung.Alert Dialog Absturz der Anwendung

Nachstehend ist der Code meiner Registrierungsklasse, die gleiche Klasse ist für Login-Aktivität geschrieben, und dort funktioniert es perfekt.

public class RegistrationActivity extends AppCompatActivity 

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


     if(sql_code.equalsIgnoreCase("0")){ 
      String resultCode= command1.getString("result"); 
      if(resultCode.equalsIgnoreCase("0")){ 
       AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create(); 
       alertDialog.setTitle("Account Created"); 
       alertDialog.setMessage("Account Created Successfully."); 
       alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Intent i= new Intent(RegistrationActivity.this, LoginActivity.class); 
         startActivity(i); 
        } 
       }); 

       alertDialog.show(); 

Manifestdatei

<activity 
     android:name=".ticketing.activities.checkout.RegistrationActivity" 
     android:screenOrientation="portrait" 
     android:label="@string/title_activity_registration" 
     android:theme="@style/AppTheme" /> 

Style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
    <item name="android:fontFamily">Roboto</item> 
</style> 

Alles ist vorhanden, aber immer noch seine mir diese Fehlermeldung zu geben, bitte leite mich, was ich bin hier falsch machen.

+1

können Sie versuchen, "AppCompatActivity" auf "Activity" zu ändern? –

+0

Mögliches Duplikat von [Android Benutzerdialog] (http://stackoverflow.com/questions/5544405/android-custom-dialog) –

+0

Mögliches Duplikat http://stackoverflow.com/questions/21814825/you-need-to-use -a-theme-appcompat-theme-oder-democendant-with-this-activity –

Antwort

1

Ersetzen Sie Ihre Dialogerstellung mit der ersten Zeile von diesem Code

AlertDialog.Builder alertDialog= new AlertDialog.Builder(context); 
       alertDialog.setTitle("Account Created"); 
       alertDialog.setMessage("Account Created Successfully."); 
       alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Intent i= new Intent(RegistrationActivity.this, LoginActivity.class); 
         startActivity(i); 
        } 
       }); 

       alertDialog.show(); 
+0

Das funktionierte perfekt gut! Danke :) Es wäre noch toller, wenn Sie erklären warum es hier funktioniert. – Kirmani88

+0

Obwohl "alertDialog.setButton" in diesem Code nicht funktioniert, muss ich das entfernen, damit es funktioniert. – Kirmani88

+0

Ich habe das wirkliche Problem im Moment nicht gefunden, aber ich habe das gleiche Problem vor ein paar Tagen konfrontiert, deshalb habe ich geantwortet –

-1

Verwenden DialogFragment für Dialoge

1
//instead of using it 
AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create(); 
//use It 
AlertDialog alertDialog = new AlertDialog.Builder(RegistrationActivity.this).create(); 
+0

Und was ist der Unterschied? –

+0

lesen Sie dies, so dass Sie bekommen können, was ist genau difference s/w sie http://stackoverflow.com/questions/22966601/what-isdifferent-between-mainactivity-this-vs-getapplicationcontext – shahid17june

+0

@sunilsunny Dialoge brauchen Aktivität Kontext und nicht Anwendungskontext.Obwohl die Ausnahme normalerweise anders wäre, wenn der App-Kontext verwendet wird. Die Frage zeigt nicht, ob OP support lib oder bare Plattform AlertDialog verwendet. – laalto

1

Verwenden Sie diesen Builder Ihren Dialog zu erstellen:

new AlertDialog.Builder(new ContextThemeWrapper(context, android.R.style.Theme_Dialog)) 

EDIT:

Eine andere Möglichkeit, das Problem zu beheben, ist ein eigenes Format zu erstellen:

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> 
<!-- Used for the buttons --> 
<item name="colorAccent">_your_color_</item> 
<!-- Used for the title and text --> 
<item name="android:textColorPrimary">_your_color_</item> 
<!-- Used for the background --> 
<item name="android:background">_your_color_</item> 

Und dann verwenden, wenn Ihr Dialog Aufbau:

new AlertDialog.Builder(new ContextThemeWrapper(context, android.R.style.MyAlertDialogStyle)) 
+0

Ich habe Ihren Code, aber das Problem ist immer noch da – Kirmani88

+0

@ Kirmani88 aktualisiert meine Antwort – Seishin