-1

Sorry rather a newbie learning android studio where I downloaded a project here to get my hands dirty with coding a camera app uploaded to a server. But somehow I couldn't get it to work after I tried compiling it, with a error for this statement in the MainActivity.java file.

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(getResources().getString(R.color.action_bar))));

Error stated: Expected resource of type string less... (Ctrl+F1) 
This inspection looks at Android API calls that have been annotated with various support annotations (such as RequiresPermission or UiThread) and flags any calls that are not using the API correctly as specified by the annotations.  Examples of errors flagged by this inspection:
Passing the wrong type of resource integer (such as R.string) to an API that expects a different type (such as R.dimen).
Forgetting to invoke the overridden method (via super) in methods that require it
Calling a method that requires a permission without having declared that permission in the manifest
Passing a resource color reference to a method which expects an RGB integer value.

Thanks in advance.

USKMobility
  • 5,721
  • 2
  • 27
  • 34
Vivian
  • 1,071
  • 5
  • 16
  • 29
  • Possible duplicate of [Get color-int from color resource](http://stackoverflow.com/questions/5271387/get-color-int-from-color-resource) – OneCricketeer Aug 11 '16 at 05:20
  • `getResources().getString(R.color.action_bar)` you are trying to get a **string** from a color resource. – Phantômaxx Aug 11 '16 at 09:08

4 Answers4

1

try this

actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));

don't forget to set color names in your project's res/values/colors.xml file

xml file

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="action_bar">#fff</color> //write the color you want here
</resources>
Abdullah
  • 935
  • 9
  • 18
  • Still having --> Error:(47, 9) error: cannot find symbol variable actionBar, what do you mean by setting color names in your project's res/values/colors.xml file? – Vivian Aug 11 '16 at 05:13
  • are you using android.support.v7 if so try ActionBar actionBar otherwise getSupportActionBar() instead of getActionBar – Abdullah Aug 11 '16 at 05:16
0

Use getSupportActionBar() if you Activity is extends AppCompatActivity and dont use Color.parseColor()

AppCompatActivity

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));

Activity

getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

You used this

getResources().getString(R.color.action_bar)

But R.color.action_bar is not a string value. Therefore the error Expected resource of type string

You have to use the correct method to get the color resource.

getResources().getColor(R.color.action_bar)

But that's deprecated, so use this

ContextCompat.getColor(MainActivity.this,R.color.action_bar)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I used the code getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(ContextCompat.getColor(this,R.color.action_bar)))); Error still occur --> Error:(47, 94) error: cannot find symbol method getColor(MainActivity,int) – Vivian Aug 11 '16 at 05:55
  • Honestly, I don't know why you are using this method to change the action bar color, but that error looks like `MainActivity` isn't extending `AppCompatActivity` – OneCricketeer Aug 11 '16 at 06:00
  • I downloaded this project from here http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/ therefore I've no idea why this problem is occurring? Please help. :( – Vivian Aug 11 '16 at 06:04
  • Firstly, the code seems wrong for that method, but there's a bunch of comments without the error that you have. Anyways, the post is over a year old, so there's likely a different way to do what it does. And I'm not at a computer to help. You say you are new to Android, so start with the basics and debugging Java errors. Reading files, using the camera, and networking with a PHP site are not introductory topics, nor required to know Android – OneCricketeer Aug 11 '16 at 06:11
0

ColorDrawable takes integer as parameter. You are trying to pass string. getString(id) method returns string. getString this method is used read string from strings.xml as getString(R.string.yourid). You should define the colors.xml under /res/values/ folder. and you can read as follows: getColor(R.color.action_bar) . This method is deprecated. You should use

ContextCompat.getColor(yourcontext,R.color.action_bar)

getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(ContextCompat.getColor(this,R.color.action_bar))));
USKMobility
  • 5,721
  • 2
  • 27
  • 34
  • Erm sorry still having error --> Error:(47, 94) error: cannot find symbol method getColor(MainActivity,int) – Vivian Aug 11 '16 at 05:30