Android: static reference / non-static method when using findViewById(ID) - java

Let me start by confessing that I come from an Erlang background and am new to Android and Java programming... to be honest object orientation is giving me a headache. :)
I am having some trouble with the sticky old chestnut: "Cannot make a static reference to the non-static method".
Basically I am writing an app that receives XML from our server and uses it to create a form to be filled in by the user.
I have managed to successfully parse the XML and create (and display) the form. I assign each EditText field it's own ID using a (very) simple algorithm that I can recreate later.
I am busy with the submit button, which makes a HTTP post back to our server with the user entered details.
My problem comes in when I try to retrieve the values that the user has entered into the form.
What I am attempting to do is loop through my IDs, open each EditText instance using EditText.findViewById(ID) and retrieve its text using getText().
When i do so however I receive the error "Cannot make a static reference to the non-static method".
Now I've done some reading and the way I understand it is that this is because I am trying access a a non-static method in a static way and in order to make it static I need to call the method of an instance rather than the class in general... the problem is that I am calling it IN ORDER to get that particular instance and I can't figure out what I should be doing differently.
I would really appreciate any help, advice or further reading that anyone has for me.
Cheers,
Bevan
p.s. Here are the relevant sections of my code
private static LinearLayout renderForm(...)
{
//Build Fields
...
//Build Buttons
...
Button BT = new Button(App);
BT.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
...
for(int j = 0; j < FFListLength; j++)
{
EditText BinField = (EditText) EditText.findViewById(20000+j);
...
}
...
}
}
}
Update:
After reading JB Nizet's answer I realized what I was doing wrong.
I have changed the line:
EditText BinField = (EditText) EditText.findViewById(20000+j);
to:
EditText binField = (EditText) lContent.findViewById(20000+j);
where lContent is the content of my view.
Thanks for the assistance.
Bevan

I'm not going to comment on the Android specific problems. Only on the general compilation error you're getting. Let's take an example:
public class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
To be able to call the getName() method, you need an instance of User:
User john = new User("John");
String johnsName = john.getName();
What you're doing is calling the getName() method without any instance of User:
String someName = User.getName();
This doesn't make sense. You don't have any user. Only the User class.
As noted in my comment; variables should start with a lower-case letter. Classes start with an upper-case letter. If you respect this convention, everything is clearer:
john.getName(); // instance method call on object john, of type User.
User.getName(); // static method call on User class.

Related

How to set and get values from an Object that exists in an ArrayList<Verbs>

I'm trying to make an array of objects called Verbs.
The Verb class has 4 Strings.
public class Verb {
String maori;
String englishPast;
String englishPresent;
String englishFuture;
}
Do I need to write get and set methods into this class in order to change these values or does android handle it for you somehow?
This seems to not be an Android problem but a java related question. You should create a Verb class like this:
public class Verb {
private String maori;
//All other strings you need
public String getMaori() {
return maori;
}
//Add a setter as well
Then you should create your Verb objects and add these objects to your array. But this has nothing to do with Android at all!

Calling an Instance method when user inputs the instance name as a String

In a small project I am working on I've gotten stuck. The user enters a command that may be "xp Speed", my command handler class finds that it wants to the XP value of the Speed Instance. In this case it needs to return the value of Skill.Speed.currentXP back to the user.
Small Part of the program:
//Example Instance initialization there is over 40 of these
Skill Speed = (new SkillSpeed(Skills.SKILL_SPEED,Skills.SKILL_SPEED_MODIFIER));
//Constructor for skill class
public Skill(String skillName, double modifier) {
this.name = skillName;
this.minLevel = Skills.MIN_SKILL_LEVEL;
this.Modifier = 1f;
this.currentLevel = (int)calculateLevel();
this.currentXP = 1;
this.leaderboard = getCurrentLeaderboard();
this.ID = getNextID();
}
Now, theres one way i could do this. by having a switch statement with case value being the string entered. However I'm sure having 40+ cases in one switch statement must be avoidable. The other theory I have had is creating a array of all current instances then iterating through that list, finding if the user inputted string is equal to the name of that instance, then returning the instance itself. This is what I came up with:
//method inside another classs that attempts to return the appropriate skill Instance
public Skill getSkillFromName(String Name) {
for(int i = 0; i < Skill.SkillArray.length; i++) {
final String SkillName = Skill.SkillArray[i].getName();
if(SkillName.equalsIgnoreCase(Name)) {
return Skill.SkillArray[i];
}
}
return null;
}
So here's what I need help with:
Creating a array of all initialized instances
Creating the method that will return Skill."InsertRandomInstanceDependingOnUserInputHere".currentXP
Fixing any problems you see in the getSkillFromName() method
Or perhaps I have overlooked a far easier way of doing this, and you can help me with that.
Thanks for the help,
BigDaveNz
If the names of the skills excatly match method names you might find the aswer at "How do I invoke a Java method when given the method name as a string?".
For finding instances by name you can still use Map's.
You can use a Map for this. E.g.:
Map<String, Skill> skills = new HashMap<String, Skill>();
To insert the values you put the values into the Map:
skills.put(skill.getName(), skill);
To retrieve your skill you can get the skill by name:
Skill skill = skills.get(name);

updating certain fields to prevent overwriting

I'm developing a Android-application using StackMob. Now I'm at the point that I want to save an object but not all his properties let's take this example.
class A extends StackMobModel
{
String UserOneInput;
String UserTwoInput;
}
Now I have two people using one instance of class A at the same time. User one puts his information in UserOneInput and user two in UserTwoInput. Now if user one saves hits information while user two already has fetched this object the situation would be
class A extends StackMobModel
{
String UserOneInput = "User one his input";
String UserTwoInput = null;
}
For user one while user two has
class A extends StackMobModel
{
String UserOneInput = null;
String UserTwoInput = null;
}
Now if player two saves his data it's saved as it is in his situation so we get
class A extends StackMobModel
{
String UserOneInput = null;
String UserTwoInput = "User two input";
}
User one his input is overwritten. I can fetch the object again before saving but if you use it on mobile networks the latency can still cause the same problem. (User two saves his information between the time that use one does a fetch and save)
I looked into the javadoc and only found a function that you can use to select certain fields but it says that won't work for saving.
Is there such an method for saving only certain fields of a class? Or is there some different model I should use to prevent overwriting?
The class does not know who is the owner, in order to know which one String to save.
This class must be decomposed to
class Answer {
String answer;
....
}
and
class Question {
Answer userOne;
Answer userTwo;
....
}
Each user has access to and saves his own Answer and the Question knows the Answers of the users.

Dynamic variable names in Java so each new object has separate name

How can I do such a thing?
String N = jTextField0.getText();
MyClass (N) = new Myclass();
Is it even possibe?
Or as my question's explains, how can I just make a method to create a new object of my specified class just with a different name each time I call it.
I really searched everywhere with no luck.
Thanks in Advance
P.S.
I wish you guys can excuse me for not being clear enough, Just to say it as it is, I made a textfield to get the name of someone who wants to make an account, and I made a class named "Customer". and a button named "Add". Now I want every time "Add" is clicked, compiler take what is in my textfield and make an object of the class "Customer" named with what it took from the textfield
It was too hard to read it in comments so I updated my question again, so sorry.
I'm stuck so bad. I suppose my problem is that I didn't "understand" what you did and only tried to copy it. This is what I wrote:
private void AddB0MouseClicked(java.awt.event.MouseEvent evt) {
String name = NameT0.getText();
Customer instance = new Customer(Name);
Customer.customers.add(instance);
and this is my Customer class:
public class Customer{
String name;
public Customer(String name){
this.name = name;
}
public String getName(){
return name;
}
static ArrayList<Customer> customers = new ArrayList<Customer>();
Variable names must be determined at compile time, they are not even part of the generated code. So there is no way to do that.
If you want to be able to give your objects names, you can use
Map<String, MyClass> map = new HashMap<>();
Add objects to the map like this (e.g):
map.put(userInput, new MyClass());
and retrieve objects like this:
MyClass mc = map.get(userInput);
I'm not entirely sure what you mean by...
how can I just make a method to create a new object of my specified
class just with a different name each time I call it
...but if I'm interpreting you correctly, I believe what you're trying to do as make MyClass accept a constructor parameter. You can do:
public class MyClass {
private String name;
public MyClass(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Then to create a new instance of MyClass, do:
String name = jTextField0.getText();
MyClass instance = new MyClass(name);
instance.getName(); // returns the name it was given
EDIT
Since you've added clarifications in the comments since I first answered this question, I thought I would update the answer to portray more of the functionality that you're looking for.
To keep track of the MyClass instances, you can add them to an ArrayList. ArrayList objects can be instantiated as follows:
ArrayList<MyClass> customers = new ArrayList<MyClass>();
Then for each MyClass instance you wish to add, do the following:
customers.add(instance);
Note that the ArrayList should not be reinstantiated for each instance that you wish to add; you should only instantiate the ArrayList once.

how to use same string in two java files

Sorry for my bad English and for maybe stupid question but I'm new in Java.
I need use same string in 2 java files for example:
In first java file I've got code for sending emails, I've got string set to default email:
public String mail = new String ("lala#gmail.com");
and I use this string in code for send email:
email.addTo(mail);
In second java file something like set up where can user set new email address I want to have same string, connected with string in first java file. When user put new email String mail will be change to new email address and in email.addTo(mail); will be use this new address
How can I do this?
use Shared Preferences, you can store it as key-value Pair. value being your email and key can be any unique string which you want to identify it with.
I'm a bit confused with the question, but I'll take a stab at it. Basically, you would like to have one String in a given file be used in multiple locations. This is easily done using class-level variables and making them publicly accessible.
For example, in the file:
EmailObject.java
public class EmailObject {
public static final String mail = "lala#gmail.com";
// The rest of your code
}
Another file can access this like so:
OtherObject.java
public void sendEmail() {
EmailMessage email = new EmailMessage();
email.addTo(EmailObject.mail);
}
Note the static and final modifiers on the original. This ensures that you do not need an actual instance of EmailObject to access the string and it also ensures that the string is never modified accidentally by some other object.
There are, of course, other ways to do this, but this one matches your code the most. This is also a very "Java" solution. Android has other ways to share data (as indicated by the other answer).
The simplest way that I would not recommend is to have a public static field:
class A {
public static String commonString;
}
class B {
public void methodThatUsesString () {
// Do stuff with the string
Log.d("I have the string", A.commonString);
}
}
If you have two Activities, and one starts another, you can send data through Intents.
The forementioned SharedPreferences way is a good solution too, if the email address is a persistent thing, a preference if you will, and not just data reqired for an operation.
You can keep a reference of one instance of a class in the otherone, and access it's fields through it:
class A {
public String commonString;
}
class B {
private final A instaceOfA;
public B (A instanceOfA) {
this.instanceOfA = instanceOfA;
}
public void methodThatUsesString () {
// Do stuff with the string
Log.d("I have the string", instanceOfA.commonString);
}
}
Or even use a getter or setter if performance is not an issue.
Many answers depending on how the string will be used.
If it's a constant string, one that will never change, never use final static String
public final static String AUTHOR_MAIL = "lala#gmail.com";
Then you can use it in a static way wherever you want.
email.addTo(MyClass.AUTHOR_MAIL);
If this String will be used in different Activities you can not access it directly (you can not tell if the other Activity is still alive). You have to use Persistence Mechanisms such as SharedPreferences or directly send needed data in your Intent.
If it's in a helper class inside your Activity, you can just use mObject.mail to get it.

Categories

Resources