I'm just starting with programming Android apps and was working through the developers guide on android.com. When trying to display a text on a second activity it says it cannot resolve the symbol EXTRA_MESSAGE as you can see here:
As far as I can tell I did every step like the guide says. I also tried copy and pasting everything but it still doesn't work. What am I missing?
You're doing a static import of extra message from some random class in MainActivity. That's wrong, don't do that. Define EXTRA_MESSAGE as a public final static String, with whatever value you want (I'd suggest "message") in your MainActivity.
There is two ways of solving it.
1) Use same static variable (Its a dirty way). At DisplayMessageActivity use android.provider.AlarmClock.EXTRA_MESSAGE.
2) This approach I recommend you. Create public static final String field at MainActivity, remove android.provider.AlarmClock.EXTRA_MESSAGE, and use MainAcitivity field at both classes. Content of this variable does not matter as long as it is unique extra key.
Related
PreferenceManager(context) is private in the package and I need to convert it to public to use. I don't know how to do so. So, can anyone help me ?
First of all, if you have an issue, you should post the actual code that's causing it.
But specifically with PreferenceManager, you can't invoke it the way you posted. If you want to instantiate it, you would need to do something like this:
String mPrefManager = mContext.getPreferenceManager()
where mContext would be the actual Context that you're trying to use
I need to write error-free, clean code for my flutter app.
In a stateless widget, if I delete const from the constructor, a yellow line error appears as seen below:
This class (or a class that this class inherits from) is marked as '#immutable', but one or more of its instance fields aren't final: PerytonDropDown.selected
I don't know how to deal with it. Can anyone help me to resolve this issue?
it is very simple, just initialize your variable with final.
Like Below :
final String text;
How do i find where static variable are used in android studio project? Is there any way to find such variables . As my project is too big and i want to remove possible memory leaks. Help me to achieve this. Thanks
I have been using this trick for a very long time now. I am sure this will definitely help you. Follow the instructions step by step and this will help...
If you are using windows, Press Ctrl + Shift + F to open entire project search...
Next, In the text to find add this Regex (public|private|protected) static and under options select case sensitive and Regular expression. This will match public static or private static or protected static.
In the scope, select the project production files
Set the file mask as *.java
Set the context as except comments and string literals
When you hit Find, you will get 1000's of results. You will find results like this:
Completely ignore the usages in generated code.... IMPORTANT
Only search in the found occurences for your static variables.
PS :
One drawback with this is that this will match static methods too... such as public static int randomMethod() { }
If your static variables don't have access specifiers such as public private or protected, just use regex as static .
1.Open your andorid project.
2.Click ctrl+shift+f
You will get a dialog box.
Then search static
You will get all static members and functions.
You can do right-click on said variable and choose Find Usages:
Result will be revealed on Find panel on bottom-right of the window.
If I know a variable's pattern such as R.id.edit_x where x (1..N), how can I get a reference to a given EditText, like findViewByID(R.id.edit_1). Is there something like an "eval" function in Dalvik ? Thanks.
Try Java reflection. Discussion on retrieving static final fields via reflection is here - Accessing Java static final ivar value through reflection
hoha's answer is good. Another thing you can do is create a look-up table that maps 1..N to the resource IDs. (Presumably you know all the resource IDs ahead of time.)
maybe, you can check roboguice. it is a ioc framework for android and it's realy easy to use. i copy some code from the sample from the project to show how to use it:
public class AstroboyMasterConsole extends RoboActivity {
#InjectView(R.id.self_destruct) Button selfDestructButton;
#InjectView(R.id.say_text) EditText sayText;
#InjectView(R.id.brush_teeth) Button brushTeethButton;
#InjectView(tag="fightevil") Button fightEvilButton; // we can also use tags if we want
}
then you can you these injected variables in your code!
Is there a way to have an global settings variable for an android application, which is accessable as well from any help java classes without giving them context?!
I try to explain what I mean.
I have an application version as string value in strings.xml
I can get its value from every android activity, but not from help java classes withought giving context
What I do now, is saving it in a static variable of my first activity, but it seems, that sometimes it will be erased and set to null.
May be I do something wrong?!
Sorry for newbie question.
And thank you in advance,
Mur
P.s.
I wrote a small tutorial for this topic, to show the solution.
A variable declared as public, static, and final will be visible to all of your classes and never get erased.
public static final String VERSION = "1.2.3.4";
You could make a public static variable in your application class that you fill with the value from strings.xml in the onCreate method. The application class is a singleton and will be the last thing that is killed as part of your app so it will always be there and if you make it public static there will be only one instance.
I'm guessing that you have a JAVA class for some common utility functions. You get the value of your string using a context in your Activity/Service and then pass in that value to the JAVA class function as a parameter.