-1

im trying to change the text of an activity 2 upon starting another (different) activity 1 with setText(), but as soon as activity 1 starts it crashes (it is opened through the click of a button). the error message is

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference

all solutions i found so far have not worked - either because they were for something else (i often saw something for a navigationdrawer, which i am not using), because they had nothing to do with my code or i had it already implemented.

activity 1:

public class ActivityTL extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tl);
        final TextView text1 = (TextView) findViewById(R.id.textView1);
        text1.setText(R.string.testB1);
        final TextView text2 = (TextView) findViewById(R.id.textView2);
        text2.setText(R.string.testB2);
        final TextView text3 = (TextView) findViewById(R.id.textView3);
        text3.setText(R.string.testB3);
        final Intent intent = new Intent(this, testA.class);
        final ImageButton imageButtonA = findViewById(R.id.imageButtonA);
        imageButtonA.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(intent);
            }
        });
    }
}

strings.xml:

    <string name="testA1">1</string>
    <string name="testA2">2</string>
    <string name="testA3">3</string>
    <string name="testB1">4</string>
    <string name="testB2">5</string>
    <string name="testB3">6</string>

if you need other information, please tell me and i will add it. thank you in advance!

Dal
  • 9
  • 1
  • The text view is in the destination activity or the activity you are moving away from? – Mehran B Dec 03 '20 at 12:49
  • Null pointer try to say that the textview is null. That can happen beacause the findviewbyid is not matching with the right one on your xml. Be sure your id matches on your xml and java file.. – Majid Ali Dec 03 '20 at 12:51
  • its in a 3rd activity; i am going from activity 1 to 2 and upon starting 2 it changes the text in activity 3 – Dal Dec 03 '20 at 12:52
  • the ids are the same too afaik, none of them are shown red so i assume that they are correct haha – Dal Dec 03 '20 at 12:54
  • Why would you wanna update a textview in an activity that is not even created? of course you get NullPointerException! activity 3 is not created so textview 3 doesn't even exist! – Mehran B Dec 03 '20 at 12:54
  • Post your xml and destination activity code with xml layout. It seams the error is in your next activity i mean testA activity – Irfan Ullah Dec 03 '20 at 12:54
  • alright the problem actually was that it isnt created, i actually didnt think of that. thank you very much! – Dal Dec 03 '20 at 12:57

1 Answers1

0

NullPointerExceptions on Views are generally related to views pointing to null value please check if the ids you are looking for are present in the Xml.
In your case Please check R.layout.activity_tl if it has all the ids your are using in Java class.

kelvin
  • 1,320
  • 1
  • 11
  • 22
  • 1
    try to explain with code examples otherwise it could be a comment.. – Majid Ali Dec 03 '20 at 12:51
  • @MajidAli not all answers need code ! Some answers are just to point op in right direction. – kelvin Dec 03 '20 at 12:58
  • `NullPointerExceptions on Views are generally related to views pointing to null value` this is quite redundant, as that's exactly what a nullpointer is, when something is pointing to null, isn't it? it's not just for views – a_local_nobody Dec 03 '20 at 14:06
  • @a_local_nobody if op knew what NullPointer is he would not post this question. So that's exactly what I wanted to tell the him + posting answers as comments would leave this kind of questions unsolved which brings unwanted attention to the question. – kelvin Dec 04 '20 at 12:34