Starting new activity Intent restarts the app and then if used again it crashes the app android studio

I have a weird dilemma right now. I have 3 activities for the app that I am currently developing. MainActivity, MainActivity2, MainActivity3. The first MainActivity along with its xml is only for splash screen . I've already configured it so that when the animation/video of the splash screen is finished, It will automatically redirect the page to MainActivity2, here the intent works perfectly. here is the code

public class MainActivity extends AppCompatActivity {

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

        VideoView mVideoView2 = findViewById(R.id.videoView2);

        String uriPath = "android.resource://"+getPackageName()+"/"+R.raw.splash;
        Uri uri = Uri.parse(uriPath);
        mVideoView2.setVideoURI(uri);
        mVideoView2.start();


       final Intent intent
                = new Intent(this, MainActivity2.class);

        mVideoView2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {

                startActivity(intent);
            }
        });
    }

}

I can navigate to the MainActivity2 via intent without any problems. But now when I created MainActivity3 wherein I should be able to access it by the "Sign Up" button, The app first restarts to splash screen then when I press it again, The app crashes.

Here is the code for MainActivity2

JAVA

public class MainActivity2 extends AppCompatActivity {

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

    public void toReg(View v){

        Intent i = new Intent(MainActivity2.this, MainActivity3.class);
        startActivity(i);

        Toast.makeText(this, "working", Toast.LENGTH_SHORT).show();
    }

}

XML - Button Sign Up

<Button
    android:id="@+id/button3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="32dp"
    android:layout_marginRight="32dp"
    android:background="@drawable/round_border3"
    android:padding="16dp"
    android:onClick="toReg"
    android:text="SIGN UP"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    android:textColor="#FFFFFF"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView2" />

Manifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity3"></activity>
        <activity android:name=".MainActivity2">

        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Here is a video of what is happening with my app.

The onclick class and toReg method works perfectly if you noticed the toast, So the only problem here I am sure of is the Intent itself, just dont know why.

Logcat says: Unable to start activity ComponentInfo



Read more here: https://stackoverflow.com/questions/66280091/starting-new-activity-intent-restarts-the-app-and-then-if-used-again-it-crashes

Content Attribution

This content was originally published by Kurt Bradley at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: