很遗憾,您的应用已停止工作

时间:2015-06-03 08:36:54

标签: java android xml android-layout android-studio

老实说,我不知道我的代码有什么问题。我在这里搜索了答案并完成了所有事情,我根本无法分辨出什么问题,请帮帮忙?

这是我的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);
}


}

我的splash.xml是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是我为main_activity.xml文件创建的额外类

    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;
        }

    }
};
}

最后是activity_main.xml文件

    <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" />

当应用程序运行时,它应该使用启动布局拉出启动屏幕五秒钟,然后显示activity_main布局,但它在切换屏幕后立即停止。请帮忙。

1 个答案:

答案 0 :(得分:0)

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

然后写

finish();

这里您首先完成您的活动,之后您将尝试调用该活动。这是不可能的。