0

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");
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
ariagno
  • 532
  • 1
  • 15
  • 29
  • You should share code for both the activity. If you are trying to print something from one activity to another, use intents to pass the data. – Hardik Trivedi Dec 30 '15 at 04:29
  • Do you have a sample code for the intent? I tried looking for a good example, but couldn't find any! – ariagno Dec 30 '15 at 04:30
  • Do you want to change the another Activity's text when you jump to it? – RockerFlower Dec 30 '15 at 04:32
  • you want to get the text from a null object reference,this is the problem :- http://stackoverflow.com/questions/27420945/java-lang-nullpointerexception-attempt-to-invoke-virtual-method-on-a-null-objec – sumit singh Dec 30 '15 at 04:32
  • http://stackoverflow.com/a/7325248/1099716 – Access Denied Dec 30 '15 at 04:34
  • That, is not the problem. I have tried using another TextView inside the main activity, and it works just fine. So its the TextView's problem, NOT a null object – ariagno Dec 30 '15 at 04:34
  • please explain, when second activity is visible – Deepak Dec 30 '15 at 04:38
  • In the button code, it runs two things. One to set the text of the TextView in another activity, then it switches to the next Activity – ariagno Dec 30 '15 at 04:40

4 Answers4

2

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);
Arnold Balliu
  • 1,099
  • 10
  • 21
1

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);
Deepak John
  • 967
  • 1
  • 7
  • 19
  • I tried this, but for some reason, the title then becomes blank, with nothing in it – ariagno Dec 30 '15 at 16:48
  • make sure that value in intent.putExtra("weapontitle",value); is not empty, for testing hardcore a string value for value like this in intent.putExtra("weapontitle","test String); – Deepak John Dec 31 '15 at 04:54
0

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);
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0
  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);
Arnold Balliu
  • 1,099
  • 10
  • 21