setText to another activity? - java

I have a button on an activity called "HomePage". When you click the button, I want it to setText to a TextView called "weaponTitle" on a separate activity, called ItemSelection. Though, when I run the setText, it gives the error:
Attempt to invoke virtual method void android.widget.TextView.setText(java.lang.CharSequence) on a null object reference
So this means it can't find the TextView "weaponTitle". Is there a good way to do fix this? I have made sure I set up everything correctly too!
Here is the sliver of code I forgot to share!
new displayPrices(weaponTitle, "Genuine Freedom Staff");

try this
Firstclass
Intent intent = new Intent(getApplicationContext,SecondActivity.class);
intent.putExtra("KEY",value);
intent.putExtra("KEY",VALUE);
startActivity(intent)
Second Activity
Intent intent =getIntent();
confirm_txt =(TextView)findViewById(R.id.txt);
String txt_put =intent.getStringExtra("KEY");
confirm_tId.setText(txt_put);

Inside oncrete of your HomePage Activity
Button yourbutton = (Button) findViewById(R.id.your_button_id);
yourbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent intent = new Intent(HomePage.this, ItemSelection.class);
intent.putExtra("weapontitle",value);
startActivity(intent);
}
});
Inside ItemSelection Activity's oncreate
Intent intent =getIntent();
String txt =intent.getStringExtra("weapontitle");
TextView weaponTitle = (TextView) findViewById(R.id.textview_weaponTitle);
weaponTitle.setText(txt);

go through this way,
pass this from your first activity,
Intent i = new Intent(Login.this, ChangePasswordActivity.class);
i.putExtra("savedUser", savedUser);
Log.e("username", "--->>" + savedUser);
startActivity(i);
in second activity , get this as below
Intent extras = getIntent();
savedUser = extras.getStringExtra("savedUser");
Log.e("savedUser", "--->>" + savedUser);
Now you can set text as you got it on other activity.
etUsername.setText(userName);

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("weaponTitle","Genuine Freedom Staff");
startActivity(intent);
In the second activity you do:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String weaponName = bundle.getString("weaponTitle");
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(weaponName);

Related

Passing textview value across activities

I am trying to pass a value to a text view into a different activity but only when the button in the activity is clicked.
Here is the activity I am trying to set the text...
public static Button yes;
public static final String TEST_KEY = "test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question19);
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
}
});
Here is the HighRiskActivity that is the destination for updating the textview's value...
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
t = (TextView) findViewById(R.id.abusedOrNah);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
String value = extras.getString(TEST_KEY);
t.setText(value);
}
My problem is that no text is printing at the desired activity, am I passing in the wrong data?? Any help would be amazing, this is the last step to the app that I am creating :D
UPDATE
I do not want the Question12Activity to be directed to the HighRiskActivity when the button is clicked. I want it to go to the next activity but still be able to pass the text onto the HighRiskActivity once the button is clicked. Sorry for the confusion, hopefully that makes more sense :)
Try to pass data this way:
Intent i = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“test”, "Data you want to pass");
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);
Now in your SecondActivity class you retrieve data as below:
Bundle bundle = getIntent().getExtras();
//get the data…
String stuff = bundle.getString(“test”);
I don't know what exactly you are trying to achieve by starting the first activity with the statements below
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
But if the Activity you want to start is the HighRiskActivity then the following should fix your issue:
Get rid of the statements related to the Question13Activity mentioned above.
and add the call to start the actual HighRiskActivity like below:
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
startActivity(i); //This line is important to start the new Activity.
So, I guess your major issue here is you didn't start the activity with the startActivity(i); call.
In Activity A you are using the internal intent bundle that is not public to you and private to the intent. In the Activity B you are asking the intent to instead look for a bundle that you provided yourself.
Try something like this in your Activity A
Intent intent = new Intent(context, YourActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("EXTRA1", "string data");
intent.putExtra(TEST_KEY, bundle);
startIntent(intent);
Here is a good writeup on the subject (see answer) Advantages of using Bundle instead of direct Intent putExtra() in Android
You dont need to define two intents in order to connect two Activities. Put the following inside onClick method
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
startActivity(i);

Setting TextView in Another Class

I have researched many problems like mine on the internet and none seem to be doing what I'm trying to do. What I am trying to do is get a textview that is currently blank in my high_risk.xml file, and make it have text based on if a button is clicked in another class. Here is what I have so far...
Question13Activity(if the yes button is clicked I want to be able to set the text in the HighRisk Activity)
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = getIntent();
String text = intent.getStringExtra("New Text");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
}
});
This is how I have the static variable t defined in my highrisk activity class...
HighRiskActivity(this is where I want the text to be set and displayed)
public static TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent myIntent = new Intent(HighRiskActivity.this, Question12Activity.class);
myIntent.putExtra("Text", "New Text");
startActivity(myIntent);
Every time I try and access the contents of t in another class and change it's text, it always returns null. Any way I can fix this from happening? Any help would be greatly appreciated :)
You can use Bounds, to get the the data to the new Activity and set the text from there:
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("<KEY-NAME>", "<TEXT>");
startActivity(myIntent);
And in the Question13Activity.class:
Intent intent = getIntent();
String text= intent.getStringExtra("<KEY-NAME>");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
UPDATE:
Use it so:
HighRiskActivity.class:
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("TestKey", "My new Text");
startActivity(myIntent);
}
});
Question13Activity.class:
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent intent = getIntent();
String text= intent.getStringExtra("TestKey");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
In order to setText() from another class (for example, from your Custom class), you need to specify the activity on which you'd like to do that.
Unless a class does have a reference of your context, you cannot call 'findViewById()'
To do that, you use Context as a parameter for custom class constructor.
Just after you have set the context will you be able to call
TextView txtView = (TextView) ((Activity)context).findViewById(R.id.text);
txtView.setText("foo");
}

sending text to new activity

i have a relative view with different images each time i click on an image i want a unique text to be sent to another activity. i have been able to achieve this for the first image but i cant get the second image to send the text to next activity.
my code for the first-activity:
b1 = (ImageView) findViewById(R.id.native_amala);
b2 = (ImageView) findViewById(R.id.native_fufu);
b3 = (ImageView) findViewById(R.id.native_jollof);
b4 = (ImageView) findViewById(R.id.native_ofada);
b5 = (ImageView) findViewById(R.id.native_porridge);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.amala_text_view);
Intent intent = new Intent(NativeFoods.this,AddToCart.class);
intent.putExtra("Amala",foodName1.getText().toString());
startActivity(intent);
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.fufu_text_view);
Intent intent2 = new Intent(NativeFoods.this,AddToCart.class);
intent2.putExtra("fufu",foodName1.getText().toString());
startActivity(intent2);
}
});
}
My second activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_to_cart);
foodView = (TextView) findViewById(R.id.cart_item_text_view);
Intent intent = getIntent();
String str = intent.getStringExtra("Amala");
foodView.setText(str);
intent.putExtra(String key, String value) is the format.
Have a unique key for all putExtra functions and use that key in getStringExtra
You should use Same KEY .
Intent putExtra (String name,
Parcelable[] value)
intent2.putExtra("value",foodName1.getText().toString());
And
Bundle bundle=getIntent().getExtras();
String str=bundle.getString("value");
foodView.setText(str);
You basically need to understand how putExtra works.
You can put some value with keys, for e.g.
intent.putExtra("Amala",foodName1.getText().toString());
Here, Amala is the key and foodName1.getText().toString() is the value.
Then in your next activity you can retrieve the value from key.
i.e. you can do,
String str = intent.getStringExtra("Amala");
In your click listener of b2 ImageView, you are mentioning the key fufu but in your second activity you are getting Amala.
You need to keep common key for all click listeners and then retrieve data from that key in second activity.
You need to keep common key for all click listeners like this way to send data
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.amala_text_view);
Intent intent = new Intent(NativeFoods.this,AddToCart.class);
intent.putExtra("data",foodName1.getText().toString());
startActivity(intent);
}
});
b2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
TextView foodName1 = (TextView) findViewById(R.id.fufu_text_view);
Intent intent2 = new Intent(NativeFoods.this,AddToCart.class);
intent2.putExtra("data",foodName1.getText().toString());
startActivity(intent2);
}
});
now receiv e data in other activty like this
foodView = (TextView) findViewById(R.id.cart_item_text_view);
Intent intent = getIntent();
String str = intent.getStringExtra("data");
foodView.setText(str);
You using the key is fufu.
intent2.putExtra("fufu",foodName1.getText().toString());
So u get the value then used that key.so change getStringExtra key name change "amala" to "fufu"
foodView = (TextView) findViewById(R.id.cart_item_text_view);
Intent intent = getIntent();
String str = intent.getStringExtra("fufu");
foodView.setText(str);
For Sending Some Text form one Activity to another activity
You can Simply use putExtra() function
putExtra(Sting Key, String value);
Let say you have String variable
String str = "Hello";
After when go to next Next Activity
Intent Intent = new Intent(getApllicationContext(),Your_Activity.class);
intent.putExtra("Hello");
startActivity(intent);
When you want this variable in Your_Activity then simply in onCreate() method
Intent intent = getIntent();
String str = intent.getStringExtra("Hello");
Use this
Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("VALUES", someValues);
startActivity(intent);
Access that intent on next activity
String s = getIntent().getStringExtra("VALUES");

How to make onClickListener receive Intent information from another activity

I'm trying to make a button use some data from another activity, I did seach, but I didn't find out how.
The onClickListener doesn't work.
Here is the code inside my onCreate:
super.onCreate(savedInstanceState);
setContentView(R.layout.show_message);
Intent intent = getIntent();
String msg = intent.getStringExtra("MESSAGE");
ScrollTextView scrolltext = (ScrollTextView) findViewById(R.id.scroll_text);
scrolltext.setText(msg);
scrolltext.setTextSize(220);
scrolltext.startScroll();
super.onResume();
DialogFragment mDialog = new GeneralDialogFragment();
mDialog.show(getSupportFragmentManager(), "Dialog TAG");
ImageButton mImageButton = (ImageButton)findViewById(R.id.image_button);
mImageButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//I'm sure this is not the to make this button works. What code should I put here to pass this data to the ScrollTextView?
scrolltext.setText(msg);
scrolltext.setTextSize(220);
scrolltext.startScroll();
}
});
This two lines that you wrote are just used to get information from the previous activity that was executing
Intent intent = getIntent();
String msg = intent.getStringExtra("MESSAGE");
If you want to use data from another activity at any time, you have to save the data in the previous activity with these lines:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("msg", "MESSAGE");
editor.commit();
and call it on your activity like this:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String yourmessage = prefs.getString("msg", null);
if (restoredText != null) {
String name = prefs.getString("msg", "No name defined");//"No name defined" is the default value.
}

Where to continue coding after created intent?

I use an intent to call a new class and show a new layout with some buttons on.
Intent intent = new Intent(MainActivity.this, Game.class);
setContentView(R.layout.game);
After this i want to show/hide some buttons on the second (game) layout.
i try to find the button:
button0 = (Button) findViewById(R.id.button0);
The thing is when i debug it never goes into the new Game-class and in my Main it can´t find my buttons because they are on the other layout.. how can i solve this?
use
Intent intent = new Intent(MainActivity.this, Game.class);
startActivity(intent); //<<<<<<< start Activity here
setContentView(R.layout.game);
instead of
Intent intent = new Intent(MainActivity.this, Game.class);
setContentView(R.layout.game);
if you want to move from MainActivity class (Activity) to Game.class (Activity)
You should move the initilization of the buttons to the onCreate method of Game Activity
public void onCreate(Bundle b){
setContentView(R.layout.game);
.
.
.
button0 = (Button) findViewById(R.id.button0);
}

Categories