Reputation:
I have two activities in my project (ChooseLevel and ShowTraining). In the first activity i have a Button, and in the second activity i have a Textview.
My problem is that when i try to click the button (and start the second activity with an intent), i want to change the text of the second activity's textview, but my app crashes. I have already searched some posts here on stackoverflow but the problem still remains.
This is the first class:
public class ChooseLevel extends AppCompatActivity {
Button firstlevel;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_level);
firstlevel = (Button)findViewById(R.id.firstlevelBtn);
tv = (TextView)findViewById(R.id.titleTextTraining); //this is the textview of the second activity
firstlevel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//start the second activity
Intent showTrainings = new Intent(ChooseLevel.this, ShowTrainings.class);
startActivity(showTrainings);
tv.setText("Some text here"); //change the text of the textview
}
});
}
The XML files of these two activities are ok. How can i solve? Thanks to everyone
Upvotes: 0
Views: 63
Reputation: 6251
tv = (TextView)findViewById(R.id.titleTextTraining);
You can't get TextView
in Activity
2 when you are in Activity
1.
You should skip to Activity
2 and change TextView
in it.
Upvotes: 0
Reputation: 1736
Do this:
ChooseLevel.java
public class ChooseLevel extends Activity {
Button firstlevel;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_level);
firstlevel = (Button)findViewById(R.id.firstlevelBtn);
firstlevel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//start the second activity
Intent showTrainings = new Intent(ChooseLevel.this, ShowTrainings.class);
showTrainings.putExtra("textValue", "Some Text");
startActivity(showTrainings);
}
});
}
}
ShowTrainings.java
public class ShowTrainings extends Activity {
Button firstlevel;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_level);
tv = (TextView)findViewById(R.id.titleTextTraining);
String str = getIntent().getStringExtra("textValue");
if(str != null) {
tv.setText(str);
}
}
}
Upvotes: 0
Reputation: 6114
One way you can setText to your second Activity is by passing your value from first Activity to the second Activity, in this case you can use the intent.putExtra()
method. So how do you do this?
Well, in the first class, ie the class from where you are passing the data,
public class ChooseLevel extends AppCompatActivity {
Button firstlevel;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_level);
firstlevel = (Button)findViewById(R.id.firstlevelBtn);
//tv = (TextView)findViewById(R.id.titleTextTraining); //this is the textview of the second activity
firstlevel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//start the second activity
Intent showTrainings = new Intent(ChooseLevel.this, ShowTrainings.class);
showTrainings.putExtra("TEXTID", "THE TEXT YOU WANT TO PASS");
startActivity(showTrainings);
}
});
}
And in the showTrainings
class, you can receive this value like:
String receive;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
receive= null;
} else {
receive= extras.getString("TEXTID");
}
} else {
receive= (String) savedInstanceState.getSerializable("TEXTID");
}
And then you can set the Received text to TextView
like:
tv = (TextView)findViewById(R.id.titleTextTraining);
tv.setText(receive);
Upvotes: 0
Reputation: 2381
Simply, you cant do what you are trying to do with findViewById().
findViewById() looks for the requested view on the content of the calling view.
So if you call it from the activity it will look for the view on the contentView you set with setContentView(), thats why its returning null.
You should pass the text to the second activity via Intent extra, then before you set the contentView of the second activity you can call findViewById() and change its text.
To pass the text via intent
Intent showTrainings = new Intent(ChooseLevel.this, ShowTrainings.class);
showTraining.putStringExtra("extrakey","YourTextHere");
then on the other side
String text = getIntent().getStringExtra("extrakey");
Hope this helps.
Upvotes: 1