Android Resources As A Member Variable In The Main Activity
Working the android hello world, I next added some strings to the strings.xml resource file. I then tried setting a member variable of my main activity class to the value of one of
Solution 1:
I don't think you can call getString on the Activity class as it may not have been properly initialised yet. You may need to split the declaration (keep it as a public String) and then the assignment (move that to onCreate).
public class MyActivity extends Activity {
/** Called when the activity is first created. */
public String myString;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myString = getString(R.string.MY_STRING); // compiles, but crashes
TextView tv = new TextView(this);
tv.setText(myString);
setContentView(tv);
}
}
Post a Comment for "Android Resources As A Member Variable In The Main Activity"