不幸的是,Audio已停止

时间:2014-07-20 18:07:46

标签: java android xml layout

我正在尝试设计主屏幕上有3个按钮的应用程序;当用户按下前两个按钮时,它播放位于原始文件夹中的不同音乐。主屏幕上的第三个按钮应该将用户带到下一个也有按钮的屏幕。

我试图在模拟器上运行我的应用程序。它在主屏幕的前两个按钮上播放音乐,但是当我点击第三个(下一个)按钮时,它会显示“不幸的是你的应用已停止”。我不知道我的代码有什么问题。

任何帮助将不胜感激。下面是我的MAIN和第二类JAVA代码,main.xmlactivity_second.xml和manifest.xml。

主要Java代码

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class Audio extends Activity implements OnClickListener {
   private MediaPlayer mp;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      setVolumeControlStream(AudioManager.STREAM_MUSIC);
      findViewById(R.id.button_1).setOnClickListener(this);
      findViewById(R.id.button_2).setOnClickListener(this);
      findViewById(R.id.button_3).setOnClickListener(this);

   }

   public void onClick(View v) {
      int resId = 1;
      switch (v.getId()) {
      case R.id.button_1: resId = R.raw.button_1; break;
      case R.id.button_2: resId = R.raw.button_2; break;
      case R.id.button_3:
         startActivity(new Intent(Audio.this,SecondActivity.class));
         break;

      }
      // Release any resources from previous MediaPlayer
      if (mp != null) {
         mp.release(); 
      }
      // Create a new MediaPlayer to play this sound
      mp = MediaPlayer.create(this, resId); 
      mp.start();
   }
}

第二类Java代码

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;   

public class SecondActivity extends Activity {      
    private MediaPlayer mp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_second);
          setVolumeControlStream(AudioManager.STREAM_MUSIC);      
       }

    public void onClick(View v) {
        int resId = 1;

        switch (v.getId()) {
          case R.id.button_4: resId = R.raw.button_4; break;
          case R.id.button_5: resId = R.raw.button_5; break;
        }

        // Release any resources from previous MediaPlayer
        if (mp != null) {               
            mp.release();   
        }

        // Create a new MediaPlayer to play this sound
        mp = MediaPlayer.create(this, resId); 
        mp.start();
    }
}

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:text="@string/directions" />

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:stretchColumns="*" >

        <Button
            android:id="@+id/button_1"
            android:text="@string/_1" />

        <Button
            android:id="@+id/button_2"
            android:text="@string/_2" />

        <Button
            android:id="@+id/button_3"
            android:text="@string/_3" />
    </TableLayout>

</LinearLayout>

activity_second.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:text="@string/directions" />

      <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:stretchColumns="*" >
    <Button
        android:id="@+id/button_4"
        android:text="@string/_4" />

    <Button
            android:id="@+id/button_5"
            android:text="@string/_5" />
  </TableLayout>
</LinearLayout>

的Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="org.example.audio"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="11" />
<application android:icon="@drawable/ic_launcher"
      android:label="@string/app_name">
    <activity android:name=".Audio"
          android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SecondActivity"/>
</application>

1 个答案:

答案 0 :(得分:1)

问题是在交换机内部的音频活动中,当您按下第三个按钮“case R.id.button_3:”时,您启动SecondActivity,但之后执行“break”,导致MediaPlayer开始播放资源默认值为1(不存在)。在特定情况下,为“返回”更改“休息”,您将没事。 原始代码:

case R.id.button_3:
     startActivity(new Intent(Audio.this,SecondActivity.class));
     break;

  }

新代码:

case R.id.button_3:
     startActivity(new Intent(Audio.this,SecondActivity.class));
     return;

  }

您应该将onClick()方法更改为以下内容,以便在进入第二个活动时音乐停止。

public void onClick(View v) {
    int resId = 1;
    // Release any resources from previous MediaPlayer
    if (mp != null) {               
        mp.release();   
    }

    // Create a new MediaPlayer to play this sound
    mp = MediaPlayer.create(this, resId); 
    mp.start();

    switch (v.getId()) {
      case R.id.button_4: resId = R.raw.button_4; break;
      case R.id.button_5: resId = R.raw.button_5; break;
    }

}

希望它有所帮助。