0

I have a problem here with variables. I created a simple Android game. The levels depend on the input the user does (birthay and birthmonth are the variables).

I made a simpler app which refers to the global variable problem only. Here are the files:

Global.java

import android.app.Application;

public class Globals extends Application {

    private String someVariable;

    public String getSomeVariable() {
        return someVariable;
    }

    public void setSomeVariable(String someVariable) {
        this.someVariable = someVariable;
    }
}

MainActivity.java:

public class MainActivity extends Activity {

    EditText editText;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.editText);
        button = (Button) findViewById(R.id.button);

        String m = editText.getText().toString();

        ((Globals) this.getApplication()).setSomeVariable(m);

    }

    public void saveName(View v) {
        Intent first = new Intent(this, FirstActivity.class);
        startActivity(first);

    }

}

FirstActivity.java:

public class FirstActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        TextView textf = (TextView) findViewById(R.id.textfirst);

        String s = ((Globals) this.getApplication()).getSomeVariable();

        textf.setText(s);

    }
}

The variable is a String so I made the EditText to a String with getText().toString() but it doesn't work. When I give the value a concrete String value it works properly also

((Globals) this.getApplication()).setSomeVariable(m); to ((Globals) this.getApplication()).setSomeVariable("message");

Any idea how to solve it?

Thank you ^^

Noah Herron
  • 630
  • 4
  • 23
Vkay
  • 495
  • 1
  • 6
  • 21
  • I am not sure but I think this is what `extras` are used for. Check this post out. http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android – Noah Herron Aug 20 '15 at 21:13
  • Does that button triggers the saveName method? If it does, then you should move String m = editText.getText().toString(); ((Globals) this.getApplication()).setSomeVariable(m); to the saveName method. Because if the Edittext doesn't have an initial value, it will set the global variable with just empty string. And yes, you should pass the desired string with the intent using putExtra function – Murat Nafiz Aug 20 '15 at 21:16
  • 1
    "but it doesn't work" = What do you mean? Does it throw an error? – iTurki Aug 20 '15 at 21:16
  • @Murat Nafiz, putting the setSomeVariable into the function did the trick, thank you ^^ – Vkay Aug 20 '15 at 21:45

1 Answers1

2

You're assigning the EditTexts content to the variable in your onCreate() method where the EditText is probably empty. Do

((Globals) this.getApplication()).setSomeVariable(m);

in your saveName(View v) method and you should be fine.

PS: "it doesn't work" is not a great description of your problem, try to be a bit more precise (what did you expect to happen, what did actually happen).

PPS: Passing your String to the next activity via intent.putExtra() would be a nicer way to pass the data.

fweigl
  • 21,278
  • 20
  • 114
  • 205