IllegalStateException:需要使用Theme.AppCompat或其后代之一

时间:2016-09-29 02:08:47

标签: java android android-layout android-appcompat

我的活动中收到以下错误消息。当我尝试实例化AlertDialog时会发生这种情况,我在下面提到过。我正在使用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
        }
    }

}

}

1 个答案:

答案 0 :(得分:1)

您正在使用AlertDialog包中的android.suppport.v7.app,这就是您需要将AppCompat主题应用于对话框的原因。