0

I need to get the string variable from one class of my project to another class,

in First class, I made a string type variable and that is Global, like this: the name of class is firstclass

 public String  firstClassVar;

and I am assigning the value to it in this method

public String requestForServerData(String strURL) throws IOException, UnknownHostException, IllegalArgumentException, Exception {
//additional code 
 firstClassVar   = myObject.getString("values");
//additional code
}

Now in second class, I am doing something like this:

  public firstclass getVar;

  public void method{
      String secondClassVar;
             secondClassVar=getVar.firstClassVar;
 }

By doing this it crashes. I have done another thing in firstclass that is

public String getStringPublically() {
       return firstClassVar;
    }

and for accessing it another class I am doing like this

   secondClassVar =getVar.getStringPublically();

and by doing this it also crashes the app.

Now I am bit new to Android, and don't know the basic way to access the string from another class.

halfer
  • 19,824
  • 17
  • 99
  • 186
Steve
  • 1,022
  • 2
  • 9
  • 30
  • You can pass the String to the method when you call it. Or store the string in stored preferences and retrieve from second location. http://stackoverflow.com/a/12074219/940834 You could also make the variable static if its appliciable. And access it directly. Or reference the instance of the class from the other class to access its variables – IAmGroot Jun 27 '14 at 10:10
  • check change to static variable and call it as classname.its_name in other class. like "public static String firstClassVar" . – Ranjit Jun 27 '14 at 10:10
  • @Doomsknight i will try it and let you know. – Steve Jun 27 '14 at 10:13

5 Answers5

2

Use public static String firstClassVar; in First Class and in Second Class use secondClassVar=FirstClass.firstClassVar;

Ahmed Nawaz
  • 1,634
  • 3
  • 21
  • 51
2

Try this..

pass values:

Intent sendStuff = new Intent(this, TargetActivity.class);
sendStuff.putExtra(key, stringvalue);
startActivity(sendStuff);

getvalues:

Intent startingIntent = getIntent();
String whatYouSent = startingIntent.getStringExtra(key, value);

IF it is Fragment

Send:

Fragment fragment = new Fragment();
 Bundle bundle = new Bundle();
 bundle.putInt(key, value);
 fragment.setArguments(bundle);

Retrieve:

Bundle bundle = this.getArguments();
 int myInt = bundle.getInt(key, defaultValue)
Giridharan
  • 4,402
  • 5
  • 27
  • 30
1

You can use SharedPreferences to set/get values in any class you want. Here is a good topic about it

Community
  • 1
  • 1
PedroHawk
  • 622
  • 5
  • 19
0

Try Using putExtra and getExtra with Intent

Rohit
  • 810
  • 2
  • 9
  • 34
  • i am using fragments, and there are issues of calling intents in fragments.. so i am not able t call an intent in fragments – Steve Jun 27 '14 at 10:12
0

The app crashed because vetVar was not initialized. Try with

  public firstclass getVar = new firstclass();

in the second class

sthor69
  • 638
  • 1
  • 10
  • 25