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));
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));
}}
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));
}}
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);
}
}
Related
Getting errors in setonclicklistener and View object
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final TextView etUsername= (TextView) findViewById(R.id.etUsername);
final TextView etPassword= (TextView) findViewById(R.id.etPassword);
final Button bLogin = (Button) findViewById(R.id.bLogin);
final TextView registerLink = (TextView) findViewById(R.id.tvRegisterHere);
registerLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(registerIntent);
ImageView myimageview =(ImageView) findViewById(R.id.myimageview);
myimageview.setImageResource(R.drawable.farm1);
}
}
);
}
Try to remove this line,
ImageView myimageview =(ImageView) findViewById(R.id.myimageview);
myimageview.setImageResource(R.drawable.farm1);
in my opinion after moving activity stop doing UI rendering related to previous activities
replce your code with
public class LoginActivity extends AppCompatActivity {
#Override
super.onCreate(savedInstanceState){
setContentView(R.layout.activity_login);
final TextView etUsername= (TextView) findViewById(R.id.etUsername);
final TextView etPassword= (TextView) findViewById(R.id.etPassword);
final Button bLogin = (Button) findViewById(R.id.bLogin);
final TextView registerLink = (TextView) findViewById(R.id.tvRegisterHere);
registerLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(registerIntent);
}
}
);
}
This is my java code:
public class jenis extends AppCompatActivity {
public Button button3;
RadioGroup radioGroup2;
RadioButton radioButton2;
DatabaseReference databaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jenis);
radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
databaseReference = FirebaseDatabase.getInstance().getReference();
final RadioButton stock = (RadioButton)findViewById(R.id.radioButton11);
final RadioButton property = (RadioButton)findViewById(R.id.radioButton12);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (stock.isChecked()){
Variable newVar = new Variable();
newVar.setAmountt("jeniss");
databaseReference.child(newVar.getAmountt()).setValue("Stock Waqf");
}
else if(property.isChecked()){
Variable newVar = new Variable();
newVar.setAmountt("jeniss");
databaseReference.child(newVar.getAmountt()).setValue("Property Waqf");
}
if(stock.isChecked()){
Intent intent = new Intent(jenis.this, stock.class);
startActivity(intent);
}
if(property.isChecked()){
Intent intent = new Intent(jenis.this, property.class);
startActivity(intent);
}
}
});
}
}
How can I display the clicked radio button value on my second activity?
I really appreciate your help, thank you.
pass the value as intent
Intent intent = new Intent(youractivity.this,secondactivity.class);
intent.putExtra("key",radiobutton.getText());
startActivity(intent);
to access it on second activity
in OnCreate() method
getIntent().getStringExtra("key");
or else
you can also store your radiobutton checked value in shared preference and
then access it on another activity... It will work like session
public class stock extends AppCompatActivity {
DatabaseReference databaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stock);
final TextView textViewOut = (TextView) findViewById(R.id.textViewOut2);
textViewOut.setText(" ");
}
public void OnCreate() {
getIntent().getStringExtra("jeniss");
}
}
what did i missed ?
as in image I've a listView of flags (for example). When I click one of this in 2nd Activity I get the text name and medium image of this flag. all work fine and as I wanted. But now in 2nd Activity when I click on the image I would like to see the respective Big Image on the 3th Activity but I can not do it. How I can do?
activitys screenshots
public class MainActivity extends AppCompatActivity
{
ListView mListView;
String [] Names = {"France", "Germany", "Italy", "USA"};
int [] Small = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4};
int [] Medium = {R.drawable.c1, R.drawable.c2, R.drawable.c3, R.drawable.c4};
int [] Big = {R.drawable.f1, R.drawable.f2, R.drawable.f3, R.drawable.f4};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listview);
MyAdapter myAdapter = new MyAdapter(MainActivity.this, Names, Small);
mListView.setAdapter(myAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent mIntent = new Intent(MainActivity.this, SecondActivity.class);
mIntent.putExtra("flagName", Names[i]);
mIntent.putExtra("flagSmall", Small[i]);
mIntent.putExtra("flagMedium", Medium[i]);
mIntent.putExtra("flagBig", Big[i]);
startActivity(mIntent);
}
});
}
this is how call the images on 2nd Activity:
public class SecondActivity extends AppCompatActivity
{
private GestureDetectorCompat gestureObject;
TextView mTextView;
ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mImageView = (ImageView) findViewById(R.id.imageView);
mTextView = (TextView) findViewById(R.id.textView);
Bundle mBundle = getIntent().getExtras();
if (mBundle != null)
{
mImageView.setImageResource(mBundle.getInt("flagMedium"));
mTextView.setText(mBundle.getString("flagName"));
}
my simple solution with mIndex:
public class SecondActivity extends AppCompatActivity
{
private GestureDetectorCompat gestureObject;
TextView mTextView;
ImageView mImageView;
int mIndex;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mImageView = (ImageView) findViewById(R.id.imageView);
mTextView = (TextView) findViewById(R.id.textView);
Bundle mBundle = getIntent().getExtras();
if (mBundle != null)
{
mImageView.setImageResource(mBundle.getInt("flagMedium"));
mTextView.setText(mBundle.getString("flagName"));
mIndex = mBundle.getInt("flagBig");
}
and I've send the value with intent to other activity.
Intent mIntent = new Intent(SecondActivity.this, ThirdActivity.class);
mIntent.putExtra("fullImg", mIndex);
startActivity(mIntent);
I've got 2 activities (well actually 4 but only 2 matter for now), the main activity and the font changing activity. The main activity sends text that the user has typed in to the font changing activity using an intent and startActivityForResult. Within the font changing activity the user can change the font of the text, as expected. What I'm trying to do is after the user has changed the font, send the text and the new font back to the main activity. But I'm not sure how to do this. I'm thinking of using some kind of bundle but I am stuck. If anyone could help me that would be greatly appreciated.
Here's my (relevant) Main Activity code:
public class MainActivity extends AppCompatActivity
{
RelativeLayout move_group;
ImageView view;
String backgroundImageName;
SeekBar seekSize;
TextView user_text;
Button button;
SeekBar tiltChange;
String temp;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user_text = (TextView) findViewById(R.id.user_text);
static final int REQUEST_FONT = 4;
public void FontChange(View view)
{
Intent fontIntent = new Intent(this, Main4Activity.class);
fontIntent.putExtra("Text", temp);
startActivityForResult(fontIntent, REQUEST_FONT);
}
//There's supposed to be an accompanying onActivityResult method as well
//but i haven't figured that part out yet
}
Here's my (relevant) Font Activity code:
public class Main4Activity extends AppCompatActivity
{
TextView user_font_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
user_font_text = (TextView) findViewById(R.id.user_font_text);
}
public void boldText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_bold);
user_font_text.setTypeface(custom_font);
}
public void italicText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_italic);
user_font_text.setTypeface(custom_font);
}
public void regularText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_regular);
user_font_text.setTypeface(custom_font);
}
public void thinText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_thin);
user_font_text.setTypeface(custom_font);
}
public void OK(View view)
{
//The intent and/or bundle would go here but I don't know what to do
}
}
public void FontChange(View view)
{
temp = user_text.getText().toString();
Intent fontIntent = new Intent(this, Main4Activity.class);
fontIntent.putExtra("Text", temp);
startActivityForResult(fontIntent, REQUEST_FONT);
}
this will fetch your string from textview to fontActivity.
Now in your FontActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
Intent intent = getIntent();
String text = intent.getStringExtra("temp");
user_font_text = (TextView) findViewById(R.id.user_font_text);
user_font_text.setText(text);
}
public void OK(View view)
{
Intent returnIntent = new Intent();
returnIntent.putExtra("result",
user_font_text.getText().toString());
returnIntent.putExtra("font",
ypur_font_type);
setResult(Activity.RESULT_OK, returnIntent);
}
Remaining part of setting font style can be done using SpannableStringBuilder. here you can check SpannableStringBuilder . Hope this helps.
I am trying to make a quiz where users choose an answer in each activity, and the final page provides an answer based on the chosen options. Therefore, I want to pass these values to the final activity, but only the last chosen value seems to show.
First Activity:
public class Quiz extends Activity {
Button btn;
RadioGroup rg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
btn = (Button) findViewById(R.id.nextBtn);
rg= (RadioGroup) findViewById(R.id.rg);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent = new Intent(getApplicationContext(), Quiz1.class);
Bundle bundle = new Bundle();
int id = rg.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
Second Activity:
public class Quiz1 extends Activity {
Button btn;
RadioGroup rg1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz1);
btn = (Button) findViewById(R.id.nextBtn1);
rg1= (RadioGroup) findViewById(R.id.rg1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg1.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent1 = new Intent(getApplicationContext(), Quiz2.class);
Bundle bundle1 = new Bundle();
int id = rg1.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle1.putString("rg1", radioButton.getText().toString());
intent1.putExtras(bundle1);
startActivity(intent1);
}
}
});
}
}
Now this follows for a total of 7 activities.. not including the final activity
Here is the 7ths (called Quiz6) activity:
public class Quiz6 extends Activity {
Button btn;
RadioGroup rg6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz6);
btn = (Button) findViewById(R.id.nextBtn6);
rg6= (RadioGroup) findViewById(R.id.rg6);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg6.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent6 = new Intent(getApplicationContext(), Final1.class);
Bundle bundle6 = new Bundle();
int id = rg6.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle6.putString("rg6", radioButton.getText().toString());
intent6.putExtras(bundle6);
startActivity(intent6);
}
}
});
}
}
You get the idea :)
Here is the FINAL activity (called Final1) here the results are show
here is the code
public class Final1 extends Activity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final1);
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
Bundle bundle1 = getIntent().getExtras();
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView.setText(bundle1.getCharSequence("rg1"));
Bundle bundle2 = getIntent().getExtras();
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView.setText(bundle2.getCharSequence("rg2"));
Bundle bundle3 = getIntent().getExtras();
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView.setText(bundle3.getCharSequence("rg3"));
Bundle bundle4 = getIntent().getExtras();
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView.setText(bundle4.getCharSequence("rg4"));
Bundle bundle5 = getIntent().getExtras();
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView.setText(bundle5.getCharSequence("rg5"));
Bundle bundle6 = getIntent().getExtras();
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView.setText(bundle6.getCharSequence("rg6"));
btn = (Button)findViewById(R.id.restartBtn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
}
}
What ends up happening once I run the program is that only "textView" ends up changing to the chosen choice on the activity right before the final activity, shown above
Any help is appreciated, thanks lots <3
An activity's intent only has one Bundle associated with it. Thus, you are only passing the last bundle to your final activity, the others are passed to the immediately following activity instead. You can fix this issue by doing
Bundle bundle = getIntent().getExtras(); //this gets the current activity's extras
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg1", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
instead of creating a new bundle each time as you do now. then pass this bundle along to the following activity. That way, each time you get a new answer, you are adding it to your bundle of answers, rather than creating a new bundle with only one item each time.
Then in your final activity, you only have to get the one bundle, and can pull all the answers out of it. NOTE: You'll want to make sure that your'e updating the correct textview with the correct text. In your question, textView is the only view that gets updated, 6 times, and the others do not get updated at all.
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));