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.
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>
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))));
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)
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)));
Related
My question is about Android/Java.
How can I call my custom view with a name such as
<input>
instead of
<org.javaforum.input>
without adding anything in the MainActivity.java?
I already read this: https://stackoverflow.com/a/17478278/16627950
But there I must add something in MainActivity.java.
I also read this: https://stackoverflow.com/a/30066376/16627950
But I don't know where do the variables mInflator and mPrefix come from.
I also read this: https://stackoverflow.com/a/13316335/16627950
But I don't know what are the programmatically steps to do so.
So how can I hook into the layout inflation in code, and handle instantiating my class for that tag myself?
mInflator and mPrefix, are custom properties declared at the top of the activity class, nothing special, in order to implement the custom layout you have to call:
val myInflater = LayoutInflater.from(this)
.cloneInContext(this)
myInflater.factory2 = MyLayoutInflater()
This from your onCreate method of the activity, I think that is not possible to add a factory without tounching the activity code
I am trying to localize my app. Therefore, I moved all strings to strings.xml. Then, I replace all hard-coded text in my code with string variables. However, I get a runtime error whenever I try to assign the resource values to my string variables.
My code looks like this:
public class MainActivity extends AppCompatActivity {
private final String MSG_STATUS =
this.getResources().getString(R.string.status);
...
}
The strings.xml looks like this:
<resources>
<string name="status">My Status</string>
</resources>
This lead to the following error:
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.xxx/com.xxx.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources android.content.Context.getResources()'
on a null object reference
All examples I have found so far had a very similar approach. Thus, I'm confused why it's not working. I guess it might have to do with the context.
Because you can't access the resources until the Activity is initialized- in onCreate, after calling super. Before then it isn't set up properly. The this pointer also won't work until the constructor has been called, and can't be used to statically initialize variables.
Basically, move all the code to onCreate.
In my code, I have something like the following:
public class MyFragment extends Fragment {
#Override
public void onAttach(Activity aActivity) {
//super.onAttach(aActivity);
}
... some more code ...
}
It appears that even with the following configuration, lint misses that this class doesn't call the super.onAttach() method from within onAttach():
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<!-- Disable the given check in this project -->
<issue id="MissingSuperCall" severity="fatal" />
</lint>
My question here is twofold:
Why is lint skipping this obvious error that should be marked as fatal?
If, as is my suspicion, that this isn't a method that lint checks (i.e. the onAttach() method of Fragment), then is there a way to get lint to flag this method as well (i.e. some deeper configuration, or, barring that, at the least an issue reporting mechanism for improving lint)?
1) Probably because you're listing it in the lint.xml file. This makes Android Lint ignore that particular error. Its mentioned here.
The lint.xml file
A configuration file that you can use to specify any lint checks that you want to exclude and to customize problem severity levels.
2) As far as flagging a method goes, you'll need to follow this. It tells you how to create a lint check for Android code. Thereafter, you could make use of the JavaScanner interface. Source can be found here. You can override the visitMethod API and visit instructions within the onAttach function. There you can check for a super.onAttach call. If not found, you can flag an error.
I have an app that shows video streaming using video views.
Since the video formats I want to show are not supported by Android Versions <2.0 I am using the vitamio library to show the videos on older devices.
However Vitamio is way slower than Android video view libraries and i don't want to use it on all devices; i just want to use it on older ones.
However the names of the libraries and the methods are the same:
i.e.
import android.media.MediaController;
import io.vov.vitamio.MediaController;
In the class I only import the android Media controller and i access the vitamio one like this:
io.vov.vitamio.widget.MediaController mediacontroller = new io.vov.vitamio.widget.MediaController(parentActivity);
which works fine until I want to access one of the methods of the vitamio library. For example:
videoView.io.vov.vitamio.widget.VideoView.setVideoURI(video);
This does not work as the correct use is:
videoView.setVideoURI(video);
If I do that then the Android media player is accessed and the code isn't correct.
How can I access a method with the same name in the same class. I want to be able to use both methods based on the device of the user.
Thanks in advance.
videoView.io.vov.vitamio.widget.VideoView.setVideoURI(video);
By doing that you are trying to call the method in a static way.
You should be declaring your videoView variable as an instance of videoView.io.vov.vitamio.widget.VideoView and then call the appropriate method :
// declaration of variable as an instance of the correct class
videoView.io.vov.vitamio.widget.VideoView videoView;
// now, use the method
videoView.setVideoURI(video);
Check your class VideoView is from type "videoView.io.vov.vitamio.widget.VideoView" and not "android.widget.VideoView".
You don't need videoView.io.vov.vitamio.widget.VideoView.setVideoURI(video), it should be videoView.setVideoURI(video) where videoView is of type io.vov.vitamio.widget.VideoView.
If it's an Object, you can cast explicitly: ((io.vov.vitamio.widget.VideoView)myObject).setVideoURI(video).
This is confusing. I'm look at the Android 2.2.2_r1 source code for the NotificationManager class, and I see the method getService() which is defined as public and static. However, eclipse is telling me:
The method getService() is undefined for the type NotificationManager
on the line
Object o = NotificationManager.getService();
My project is building against Android 2.2/ API Level 8. I tried to use reflection to see the method names and modifiers, and sure enough, I got back
public static getService
Am I missing something here? Why would eclipse tell me this method doesn't exist?
You will find a very detailed answer in this post.
In short: Because you compile against the android.jar, which has all hidden methods (like the one you are trying to access) removed. They will only be there on runtime, for internal android usage.
But since you perhaps also need it. The right way to access the NotificationManager is via the getSystemService method of a context:
NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
With context being a valid context (like your current activity).