• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

App Inventor Button makes the to return to the main activity

Hey. I want to make a button which makes the app to move to another activity.
I did it a few times with this app with no problem, but for some reason, this simple button, when I click it, there is a black screen for half a second and it returns to the main activity.

Java:
  explanation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent toExplanation = new Intent(getApplicationContext(), ChoiceExplanationActivity.class);

                startActivity(toExplanation);
            }
        });

This is what I'v done in the activity which the button is in.

Java:
public class ChoiceExplanationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choice_explanation);

        TextView choiceExplanation = findViewById(R.id.explanation);


        choiceExplanation.setText("ok");
    }
}

And this is the activity which it should lead too.
When I looked at the log when clicking the button, I found this line which I think is the problem.

Code:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appdeveloping/com.example.appdeveloping.ChoiceExplanationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

Thanks for the help.
 
Found my problem.
I have accidentally initialized the choiceExplanation text view with "explanation" which is a button and not a text view.
I'm stuiped
 
Back
Top Bottom