Unfortunately, your app stopped working

Question!

I honestly do not know what is wrong with my code. I have searched for answers here and have done everything and i simply cannot tell what is wrong, Please help?

This is my MainActivity.java,

    package com.example.myname.easternmusic;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import java.util.Timer;
    import java.util.TimerTask;


    public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    TimerTask task=new TimerTask() {

        @Override
        public void run() {
            finish();
            startActivity(new Intent(MainActivity.this, Main.class));
        }
    };
    Timer opening=new Timer();
    opening.schedule(task,5000);
}


}

My splash.xml which is the layout for the MainActivity.java

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sounds of the East"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:textSize="20dp"
    android:textColor="#000000"
    android:textStyle="bold"
    android:gravity="center_horizontal"
    android:background="@drawable/bell"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true" />

My Main.java which is the extra class I created for the main_activity.xml file

    package com.example.myname.easternmusic;

    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;


    public class Main extends Activity {

    Button btBamboo, btPalace;
    MediaPlayer mpBamboo, mpPalace;
    int playing;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView imageView=(ImageView)findViewById(R.id.imageView);
    ImageView imageView1=(ImageView)findViewById(R.id.imageView2);
    btBamboo=(Button)findViewById(R.id.button1);
    btPalace=(Button)findViewById(R.id.button2);
    btBamboo.setOnClickListener(bBamboo);
    btPalace.setOnClickListener(bPalace);
    mpBamboo=new MediaPlayer();
    mpBamboo=MediaPlayer.create(this,R.raw.bamboo);
    mpPalace=new MediaPlayer();
    mpPalace=MediaPlayer.create(this,R.raw.palace);
    playing=0;
}
Button.OnClickListener bBamboo=new Button.OnClickListener(){

    @Override
    public void onClick(View v) {
    switch (playing){
        case 0:
            mpBamboo.start();
            playing=1;
            btBamboo.setText("Pause Bamboo Song");
            btPalace.setVisibility(View.INVISIBLE);
            break;
        case 1:
            mpBamboo.pause();
            playing=0;
            btBamboo.setText("Play Bamboo Song");
            btPalace.setVisibility(View.VISIBLE);
            break;
    }
    }
};
Button.OnClickListener bPalace=new Button.OnClickListener(){

    @Override
    public void onClick(View v) {
        switch (playing){
            case 0:
                mpPalace.start();
                playing=1;
                btPalace.setText("Pause Palace Song");
                btBamboo.setVisibility(View.INVISIBLE);
                break;
            case 1:
                mpPalace.pause();
                playing=0;
                btPalace.setText("Play Palace Song");
                btBamboo.setVisibility(View.VISIBLE);
                break;
        }

    }
};
}

and finally, the activity_main.xml file

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Splash">

<ImageView
    android:layout_width="320dp"
    android:layout_height="150dp"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:src="@drawable/band" />

<Button
    android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:text="Play Bamboo Song"
    android:id="@+id/button1"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:textSize="22dp"
    android:layout_marginBottom="10dp" />

<ImageView
    android:layout_width="320dp"
    android:layout_height="150dp"
    android:id="@+id/imageView2"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:src="@drawable/drums" />

<Button
    android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:text="Play Palace Song"
    android:id="@+id/button2"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:textSize="22dp"
    android:layout_marginBottom="10dp"
    android:layout_alignParentStart="false" />

When the app is run, it should pull up a splash screen with the splash layout for five seconds, and then display the activity_main layout, but it stops right after it switches screens. Please help.

By : droidnoob


Answers
write

startActivity(new Intent(MainActivity.this, Main.class));

then write

finish();

here You first finish your activity and after that you are try to call that activity. That is not possible.

By : Abhishek


This video can help you solving your question :)
By: admin