ariagno
ariagno

Reputation: 601

setText to another activity?

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");

Upvotes: 0

Views: 6533

Answers (4)

Deepak John
Deepak John

Reputation: 967

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);

Upvotes: 1

Arnold Balliu
Arnold Balliu

Reputation: 1119

  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);

Upvotes: 0

user5722294
user5722294

Reputation:

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);

Upvotes: 2

Amit Vaghela
Amit Vaghela

Reputation: 22945

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);

Upvotes: 0

Related Questions