2016-08-12 45 views
-3

這已被問了很多次,並已被回答,我已經解決了這個問題,但這次卡住了,這是在一個地方工作,但在另一個活動中,它給了我以下錯誤消息。警報對話框崩潰應用程序

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 Custom Dialog]可能的重複(http://stackoverflow.com/questions/5544405/android-custom-dialog) –

+0

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

回答

1

於第一線,從這個代碼

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

That Worked完美無瑕!謝謝:) 如果你解釋一下,它會更加真棒爲什麼在這裏工作。 – 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

閱讀這個,你可以得到什麼是真正diff/b /他們 http://stackoverflow.com/questions/22966601/what-is-different-between-mainactivity-this-vs-getapplicationcontext – shahid17june

+0

@sunilsunny對話框需要活動上下文而不是應用上下文。雖然在使用應用上下文的情況下通常情況下會有所不同。問題是沒有顯示OP是使用支持庫還是裸機平臺AlertDialog。 – laalto

1

使用DialogFragment使用該生成器來創建您的對話框:

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

編輯:

另一種方式來解決這個問題是創建一個自定義樣式:

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

然後構建對話框時使用它:

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

我用過你的代碼,但問題仍然存在 – Kirmani88

+0

@ Kirmani88更新了我的答案 – Seishin