0

I am trying to create an activity with a button that runs color picker for my app, here is my Activity code :

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

    }

everything is working fine but when i hit the button i get this error :

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)

im also adding my styles:

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

and Style v14

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

and v11

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

and i also changed the activity to Appcompactactivity what should i do ?

Stanojkovic
  • 1,612
  • 1
  • 17
  • 24

2 Answers2

1

Go to your styles and put the parent

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

instead of

parent="Theme.AppCompat"

Or you can use AppCompat for backward compatibility.

mubeen
  • 813
  • 2
  • 18
  • 39
  • i have 3 style.xml ,v11, and v14 which one ? – Soheil Afshari Mar 29 '16 at 09:53
  • values-v11 : Version is 11 or higher and values-v14 : Version is 14 or higher. Style.xml is the default folder and it will be the last to be matched, covering other APIs levels not covered by another values-xx folder. – mubeen Mar 29 '16 at 10:22
  • You are getting this exception because you are not using AppCompat. You can add appcompat dependencies, to your project will also solve the problem. You can not use AppCompat right now. Change it to as mention in answer – mubeen Mar 29 '16 at 10:24
1

Your logcat throwing

 java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
  • At First extends AppCompatActivity instead of Activity

public class color extends AppCompatActivity {

You can share your Application style.

  1. First section parent="@android:style/Theme.Holo.Light">

  2. another section parent="android:Theme.Holo.Light"> in v11

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

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198