2016-09-29 98 views
0

我在我的活动中收到以下错误。当我尝试实例化一个AlertDialog时发生,我在下面提到。我使用的是程序兼容性的主题,所以我不知道为什么这个错误被触发:IllegalStateException:需要使用Theme.AppCompat或其后代

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
                       at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343) 
                       at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312) 
                       at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277) 
                       at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:80) 
                       at android.support.v7.app.AlertController.installContent(AlertController.java:214) 
                       at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257) 
                       at android.app.Dialog.dispatchOnCreate(Dialog.java:397) 
                       at android.app.Dialog.show(Dialog.java:295) 
                       at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:953) 
                       at com.troychuinard.fanpolls.NewImageActivity$MyAdapter$1.onClick(NewImageActivity.java:243) 
                       at android.view.View.performClick(View.java:5210) 
                       at android.view.View$PerformClick.run(View.java:21294) 
                       at android.os.Handler.handleCallback(Handler.java:739) 
                       at android.os.Handler.dispatchMessage(Handler.java:95) 
                       at android.os.Looper.loop(Looper.java:148) 
                       at android.app.ActivityThread.main(ActivityThread.java:5527) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

不过,我实际应用的主题,我的活动:

值-V21

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:windowContentTransitions">true</item> 
    <item name="android:windowAllowEnterTransitionOverlap">true</item> 
    <item name="android:windowAllowReturnTransitionOverlap">true</item> 
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item> 
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item> 
</style> 

当我尝试以编程方式实例化TextInputLyout时发生该错误:

public class CreateActivity extends AppCompatActivity { 

private String mParam1; 
private String mParam2; 

private Button mAddImageButton; 

private ImageView mAddAnswersButton; 
private ImageView mImagePreview; 
private String mSpinnerPosition; 

private EditText mCreatePollQuestion; 

private View mRootView; 

private TextInputLayout mCreatePollAnswer; 
private EditText mCreatePollAnswerEditText; 
private TextView mCreatePollAnswerCounter; 
private int mNumberOfPollAnswersCreatedByUser; 
private ViewGroup mEditTextAnswerLayout; 
private FloatingActionButton mSubmitPollCreation; 

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

    mAddImageButton = (Button) findViewById(R.id.add_image_button); 
    mAddAnswersButton = (ImageView) findViewById(R.id.add_answers_button); 
    mImagePreview = (ImageView) findViewById(R.id.preview_image); 
    mCreatePollQuestion = (EditText) findViewById(R.id.create_poll_question_editText); 
    mCreatePollAnswerCounter = (TextView) findViewById(R.id.create_poll_answer_counter_TextView); 
    mEditTextAnswerLayout = (ViewGroup) findViewById(R.id.create_poll_questions_answer_layout); 
    mSubmitPollCreation = (FloatingActionButton) findViewById(R.id.submit_poll_FAB); 
    mNumberOfPollAnswersCreatedByUser = 2; 
    mCreatePollAnswerCounter.setText(String.valueOf(mNumberOfPollAnswersCreatedByUser)); 
    for (int i = 0; i < mNumberOfPollAnswersCreatedByUser; i++){ 
     createAnswerChoice(i+1); 
    } 
    mAddAnswersButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mNumberOfPollAnswersCreatedByUser++; 
      if (mNumberOfPollAnswersCreatedByUser > 5){ 
       Toast.makeText(getApplicationContext(), R.string.max_create_answers, Toast.LENGTH_SHORT).show(); 
       return; 
      } 
      createAnswerChoice(mNumberOfPollAnswersCreatedByUser); 
      mCreatePollAnswerCounter.setText(String.valueOf(mNumberOfPollAnswersCreatedByUser)); 
     } 
    }); 

    mSubmitPollCreation.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (mNumberOfPollAnswersCreatedByUser > 5){ 
       mNumberOfPollAnswersCreatedByUser = 5; 
      } 
      for (int i = 0; i < mNumberOfPollAnswersCreatedByUser; i++){ 
       EditText editText = (EditText) mEditTextAnswerLayout.findViewWithTag(getResources().getString(R.string.created_answer_editText_id)+String.valueOf(i+1)); 
       String editTextInputForAnswer = String.valueOf(editText.getText()); 
       Toast.makeText(getApplicationContext(),editTextInputForAnswer,Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 

    mAddImageButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent toImageSearch = new Intent(getApplicationContext(), NewImageActivity.class); 
      startActivityForResult(toImageSearch, 1); 
     } 
    }); 


} 

//programatically create editText based 
private void createAnswerChoice(int answerNumber) { 
    EditText editText = new EditText(getApplicationContext()); 
    editText.setHint(getResources().getString(R.string.answer_text) + " " + answerNumber); 
    editText.setSingleLine(true); 
    editText.setImeOptions(EditorInfo.IME_ACTION_DONE); 
    String editTextID = ((getResources().getString(R.string.created_answer_editText_id))+String.valueOf(answerNumber)); 
    editText.setTag(editTextID); 
    Toast.makeText(getApplicationContext(), editTextID, Toast.LENGTH_SHORT).show(); 
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    editText.setLayoutParams(layoutParams); 
    TextInputLayout newAnswer = new TextInputLayout(getApplicationContext()); 
    newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
    newAnswer.addView(editText, layoutParams); 
    mEditTextAnswerLayout.addView(newAnswer); 
} 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 1) { 
     if(resultCode == CreateActivity.RESULT_OK){ 
      String resultURL =data.getStringExtra("result"); 
      mAddImageButton.setVisibility(View.INVISIBLE); 
      Picasso.with(getApplicationContext()) 
        .load(resultCode) 
        .into(mImagePreview); 
     } 
     if (resultCode == CreateActivity.RESULT_CANCELED) { 
      //Write your code if there's no result 
     } 
    } 

} 

}

+0

设备的版本是什么?值-V21仅在版本API = 21时生效,较低版本的主题应该在值中进行配置。 – Bennyhuo

+0

我已经在两个版本上测试过,它不是在< or > 21上渲染的,它与编程的TextInputLayouts有关,我无法弄清楚 – tccpg288

+1

它在关联的答案中。在实例化'View's,'Dialog's等时不要使用'getApplicationContext()' –

回答

1

您使用android.suppport.v7.app包装中的AlertDialog,这就是为什么您需要将AppCompat主题应用于对话框。

相关问题