How do i make a button show a new page and add a specified text of the textview in the new page? [closed]

You should pass the string value to the NewActivity by using

newActIntent.putExtra("someKey", myText); in your first Activity.

Your onClick method should be something like this:

public void onClick(View view) {

        String myText = "this is the text";
        Intent newActIntent = new Intent(view.getContext(),NewActivity.class);
        newActIntent.putExtra("someKey", myText);
        startActivity(newActIntent);
}

Then on your NewActivity you should retrieve the someKeyvalue you stored in the first activity and use the extracted value to set the TextView text:

String textValue = intent.getExtras().getString("someKey");
TextView tView = (TextView) findViewById(R.id.tView);
tView.setText(textValue);

The reason why you’re getting the NullPointerException when trying to set the text in your first activity is because findViewById(id)will try to find a view with the specified ID in the layout of your current Activity, not on every Activity you have. For this reason you should first start the Activity where the TextView is declared, retrieve the TextView by using the findViewById(id)method and then set the text, based on the value you passed using the Intent.

Browse More Popular Posts

Leave a Comment