Home>

I want to transition the screen from Main to Category

When you click the "Start" button on the Main screen, the application will end and the following error message will be displayed.

unfortunately, QuestionApplication has stopped.
Corresponding source code

activity_main.xml

<? xml version = "1.0" encoding = "utf-8"?><TextView
        android: id = "@ + id/textPage"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: text = "Main"
        android: textSize = "30sp"
        app: layout_constraintLeft_toLeftOf = "parent"
        app: layout_constraintTop_toTopOf = "parent" /><Button
        android: id = "@ + id/buttonStart"
        android: layout_width = "250sp"
        android: layout_height = "130sp"
        android: text = "start"
        android: textSize = "50sp"
        app: layout_constraintTop_toTopOf = "parent"
        app: layout_constraintBottom_toBottomOf = "parent"
        app: layout_constraintLeft_toLeftOf = "parent"
        app: layout_constraintRight_toRightOf = "parent" /></androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.questionapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;import android.widget.Button;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
    Button btnS;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        btnS = (Button) findViewById (R.id.buttonStart);// Start button
        btnS.setOnClickListener (new View.OnClickListener () {
            public void onClick (View view) {
                Intent intent = new Intent ();
                intent.setClassName ("com.example.questionapp", "com.example.questionapp.CategoryActivity");
                startActivity (intent);
            }
        });
    }
}

activity_category.xml

<? xml version = "1.0" encoding = "utf-8"?><TextView
        android: id = "@ + id/textPage"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: text = "Category"
        android: textSize = "30sp"
        app: layout_constraintLeft_toLeftOf = "parent"
        app: layout_constraintTop_toTopOf = "parent" /></androidx.constraintlayout.widget.ConstraintLayout>

CategoryActivity.java

package com.example.questionapplication;
import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.widget.TextView;
public class CategoryActivity extends AppCompatActivity {
    TextView txtP;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_category);
        txtP = (TextView) findViewById (R.id.textPage);
    }
}

AndroidManifest.xml

<? xml version = "1.0" encoding = "utf-8"?><manifest xmlns: android = "http://schemas.android.com/apk/res/android"
    package = "com.example.questionapplication">         </activity>                <intent-filter>                            <category android: name = "android.intent.category.LAUNCHER" />        </intent-filter>    </activity></application></manifest>
What I tried

Re-enter and confirm AndroidManifest.xml
AVD Manager Data Erase

Supplementary information (FW/tool version, etc.)

Android Studio 4.1
AVD: Nexus7 (2012) API23
SDK: Android 6.0 (Marshmallow) API level 23

  • Answer # 1

    The package name is

    package com.example.questionapplication;

    However, the package name and fully qualified class name set in Intent are

    intent.setClassName ("com.example.questionapp", "com.example.questionapp.CategoryActivity");

    It has become.question applicationWhenquestionappIt's different. This is the reason why it cannot be migrated normally.

    intent.setClassName ("com.example.questionapplication", "com.example.questionapplication.CategoryActivity");


    You should be able to migrate if you change to.

  • Answer # 2

    Intent intent = new Intent (getApplication (), CategoryActivity.class);


    Please try with.