您需要在此活动中使用Theme.AppCompat主题(或后代)

时间:2016-03-29 09:33:12

标签: android android-appcompat

我正在尝试使用为我的应用运行颜色选择器的按钮创建活动, 这是我的活动代码:

public class color extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color);
        final ImageView imgbck= (ImageView) findViewById(R.id.imageView4);
        ImageButton imgbt= (ImageButton) findViewById(R.id.colorpickerbt);
        imgbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ColorPickerDialogBuilder
    .with(getApplicationContext())
    .setTitle("Choose color")
    .wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
    .density(12)
    .setOnColorSelectedListener(new OnColorSelectedListener() {
        @Override
        public void onColorSelected(int selectedColor) {
            String ss=Integer.toHexString(selectedColor);
            Toast.makeText(getApplicationContext(),ss,Toast.LENGTH_SHORT).show();
        }
    })
    .setPositiveButton("ok", new ColorPickerClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) {
            imgbck.setBackgroundColor(selectedColor);
        }
    })
    .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    })
    .build()
    .show();
            }
        });

    }

一切正常,但当我点击按钮时出现此错误:

FATAL EXCEPTION: main
Process: com.soheil.prolightfa, PID: 31576
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:310)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:76)
at android.support.v7.app.AlertController.installContent(AlertController.java:213)

我还添加了我的风格:              

<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

和Style v14

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
    </style>

和v11

  <style name="AppBaseTheme" parent="Theme.AppCompat">
        <!-- API 11 theme customizations can go here. -->
    </style>

我还将活动更改为Appcompactactivity 我该怎么办?

2 个答案:

答案 0 :(得分:1)

转到您的样式并放置父

parent="@android:style/Theme.Holo.Light"

而不是

parent="Theme.AppCompat"

或者您可以使用AppCompat来实现向后兼容。

答案 1 :(得分:1)

您的logcat投掷

 java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
  • 首先扩展AppCompatActivity而不是Activity
  

公共类颜色扩展了AppCompatActivity {

您可以分享您的应用程序样式。

  1. 第一部分parent="@android:style/Theme.Holo.Light">

  2. parent="android:Theme.Holo.Light">

  3. 中的另一部分v11

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