简体   繁体   中英

In Android Studio MainActivity4 wont open

I am new to programming so please help. I am practicing value transfer in Activities. When I put name and click Continue it does not open MainActivity4. I tried to look up but as I said I am so new I don't even know how to search this. There is no error message. It just restarts app or if I dont put any value in PersonName it just crashes. Please help :D

 public class MainActivity3 extends AppCompatActivity { String userName; //String userAge; EditText PersonName; //EditText PersonAge; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); PersonName =findViewById(R.id.PersonName); //PersonAge =findViewById(R.id.PersonAge); userName=""; //userAge=""; } public void Continue(View view){ userName=PersonName.getText().toString(); //userAge=PersonAge.getText().toString(); Intent intent= new Intent(getApplicationContext(),MainActivity4.class); intent.putExtra("userInput",userName); // intent.putExtra("userInputAge",userAge); startActivity(intent); } }
 package com.talhakotenapps.word_based_rpg; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity4 extends AppCompatActivity { TextView textView2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main4); textView2=findViewById(R.id.PersonName); Intent intent=getIntent(); String userName=intent.getStringExtra("userInput"); textView2.setText(userName); } public void Back(View view){ Intent intent=new Intent(getApplicationContext(),MainActivity3.class); startActivity(intent); } }

Here is XML files of MainActivity4!

  <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity4">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="77dp"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="30dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="Back"
        android:text="Button"
        tools:layout_editor_absoluteX="160dp"
        tools:layout_editor_absoluteY="198dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

In the onCreate of your MainActivity4.java file, you're doing the following line:

textView2 = findViewById(R.id.PersonName);

but if we look at the XML file of your activity, there's no View with id PersonName .

Maybe you were referring to textView2 ?

textView2 = findViewById(R.id.textView2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM