Search code examples
androidstringputextra

Android .putExtra (not responding)


I need to pass a String created from 'Activity A' to 'Activity B' so that I can display it in a TextView. The problem is that the code causes Android to not respond, its identical to the other tutorials online.

Thanks for any feedback.

Activity A.onCreate()

check_button = (Button) findViewById(R.id.check_button);
    check_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            Intent i = new Intent(AddActivity.this, DetailActivity.class);
            String hash = text_hash.toString();
            i.putExtra("hash" , hash);
            startActivity(i);
        }
    });

Activity B.onCreate()

Bundle extras = getIntent().getExtras();
if (extras != null)
{
    passedHash = (String) getIntent().getExtras().getString("hash");
    hash.setText(passedHash);
}

stack trace:

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

Solution

  • According to your log, it seems that you didn't initialize your TextView in Activity B. Do this before setting the text to your TextView:

    TextView hash = (TextView)findViewById(R.id.hash_textview);