2016-08-12 13 views
-3

これは何度も尋ねられていますが、これまでにこの問題を解決しましたが、この時間がかかりました。これは1か所で動作していますが、アラートダイアログでアプリケーションがクラッシュする

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 

私の登録クラスのコードは、ログインアクティビティ用に同じクラスが書かれています。

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

マニフェストファイル

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

すべてが整備されているが、それでもその私に、このエラーメッセージを与えて、親切に私は何私を導いここで間違っている。

+1

を、あなたは、「アクティビティ」に「AppCompatActivity」を変更してみてくださいことはできますか? –

+0

[Androidカスタムダイアログ](http://stackoverflow.com/questions/5544405/android-custom-dialog)の可能な複製 –

+0

可能な複製http://stackoverflow.com/questions/21814825/you-need-to-use -a-theme-appcompat-theme-or-descend-with-this-activity –

答えて

1

extends Activityにこのコード

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

それは完璧にうまくいった!ありがとう:) あなたが説明するなら、それはもっと素晴らしいでしょうなぜここで働いているのですか? – Kirmani88

+0

'alertDialog.setButton'はこのコードでは動作しませんが、動作させるためにはこれを削除する必要があります。 – Kirmani88

+0

私は今実際の問題を見つけられませんでしたが、私は数日前に同じ問題に直面したので、私は答えました –

-1

ダイアログ

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

そして違いは何ですか? –

+0

これを読んで、正確に何かを得ることができます。b/w http://stackoverflow.com/questions/22966601/what-is-different-between-mainactivity-this-vs-getapplicationcontext – shahid17june

+0

@sunilsunnyダイアログにはアクティビティコンテキストが必要ですアプリケーションコンテキストではありません。通常、アプリコンテキストが使用された場合は例外が異なります。問題は、OPがサポートlibを使用しているのか、それともプラットフォームのないAlertDialogを使用しているのかを示していません。 – laalto

1

使用あなたのダイアログを作成するには、このビルダーのために使用DialogFragment

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

EDIT:問題を修正する

もう一つの方法は、カスタムスタイルを作成することです。

<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> 
その後、3210

そして、あなたのダイアログを構築する際にそれを使用します。

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

私はあなたのコードを使用しましたが、問題はまだそこにあります – Kirmani88

+0

@ Kirmani88は私の答えを更新しました – Seishin

関連する問題