I'm trying to send a variable from one activity to another. When I try to test the app on my phone, it crashes and says the app has stopped working.
Code from sending side
if (id==R.id.action_cart){
Intent good1 = new Intent(MainActivity.this, Scroll.class);
good1.putExtra("intVariableName", 5);
startActivity(good1);
TextView hides = (TextView) findViewById(R.id.textView3);
hides.setVisibility(View.INVISIBLE);
}
and code on receiving side
public class Scroll extends AppCompatActivity {
TextView btn = (TextView) findViewById(R.id.text);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(intValue);
}}
If you know what the issue is please let me know. I've spent hours looking for a solution. Thank you
you need to move this TextView btn = (TextView) findViewById(R.id.text);
inside oncreate
because before oncreate
there is no layout attached to Scroll
activity and finding textview
by using this findViewById(R.id.text)
will result in a failure hence crash
public class Scroll extends AppCompatActivity {
TextView btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
btn = (TextView) findViewById(R.id.text)
//^^^^
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(""+intValue);
}
}
Note : Use appropriate naming convention for clarity , like textViewVar
for TextView
instead of btn
and convert your int
value to text
using ""+value
because TextView
allow only String
to use
I think the problem is in this call: btn.setText(intValue);
: string resource ID with value 5 does not exist. Try replacing it with btn.setText(Integer.toString(intValue));
In your activity
public class Scroll extends AppCompatActivity {
TextView btn ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
btn = (TextView) findViewById(R.id.text); // initialize your button here
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(String.valueOf(intValue));
}}
Try this
if (id==R.id.action_cart){
//hide views before moving
TextView hides = (TextView) findViewById(R.id.textView3);
hides.setVisibility(View.INVISIBLE);
Intent good1 = new Intent(MainActivity.this, Scroll.class);
good1.putExtra("intVariableName", 5);
startActivity(good1);
}
and in Recieving activity put this in oncreate
btn = (TextView) findViewById(R.id.text);
Like this
public class Scroll extends AppCompatActivity {
TextView btn ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
btn = (TextView) findViewById(R.id.text);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(intValue);
}}
change a line to: btn.setText(String.valueOf(intValue));
public class Scroll extends AppCompatActivity {
TextView btn = (TextView) findViewById(R.id.text);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(String.valueOf(intValue));
}}
Replace your code by this
public class Scroll extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
TextView btn = (TextView) findViewById(R.id.text);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(intValue);
}
}
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.