So I'm trying to change the value of a string (it's called "string1") in the class Favoutires.
I'm using the below code. Any ideas why it's not working and maybe what will work?
String Athenry = "Athenry";
Favourites favourites = new Favourites();
favourites.string1 = Athenry;
Below is my Favorites Class
public class Favourites extends ActionBarActivity {
String string1;
String st2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fav);
final Button but1 = (Button) findViewById(R.id.button1);
one way is to make the String string1 static
public class Favourites extends ActionBarActivity {
public static String string1;
String st2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fav);
final Button but1 = (Button) findViewById(R.id.button1);
and
Favourites.string1 = Athenry;
Because you did declare the access modifier for the variable string1. It is private by default. You have to make it public like this:
public String string1;
But that is a bad practice. You would want to have getter and setters for these kinds of jobs. So basically this:
private String string1;
public String setString1(String s){
string1 = s;
}
And you access it by calling the method
Object.setString("Your String");
you chould make a setter for that string
public class Favourites extends ActionBarActivity {
String string1;
public void setString1(String value){
string1=value;
}
and then call it when you need
String Athenry = "Athenry";
Favourites favourites = new Favourites();
favourites.setString1(Athenry);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.