我有一个应用程序,很多用户已经帮助我到目前为止,我真的很感激。现在,当案件在下面被删除时,我有一个强制关闭问题。当我点击按钮2时,它将加载Ship.Class,一切都像我想要的那样工作。当我点击button1它将不会加载Ocean.Class并强制关闭。 Ocean.Class是Ship.Class的镜像,除了变量名已经更新以反映不同的类。使用变量(a)就像我在开关中不是允许的东西?
package com.androidsleepmachine.gamble;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class Ocean extends Activity implements View.OnClickListener {
public static final Integer[] TIME_IN_MINUTES = { 30, 45, 60, 180, 360 };
public MediaPlayer mediaPlayer;
public Handler handler = new Handler();
public Button button1;
public Spinner spinner1;
// Initialize the activity
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.ocean);
button1 = (Button) findViewById(R.id.btn1);
button1.setOnClickListener(this);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
android.R.layout.simple_spinner_item, TIME_IN_MINUTES);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
}
// Play the sound and start the timer
private void playSound(int resourceId) {
// Cleanup any previous sound files
cleanup();
// Create a new media player instance and start it
mediaPlayer = MediaPlayer.create(this, resourceId);
mediaPlayer.start();
// Create the timer to stop the sound after x number of milliseconds
int selectedTime = TIME_IN_MINUTES[spinner1.getSelectedItemPosition()];
handler.postDelayed(runnable, selectedTime * 60 * 1000);
}
// Handle button callback
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
playSound(R.raw.ocean_birds);
break;
}
}
// Stop the sound and cleanup the media player
public void cleanup() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
// Cancel any previously running tasks
handler.removeCallbacks(runnable);
}
// Runnable task used by the handler to stop the sound
public Runnable runnable = new Runnable() {
public void run() {
cleanup();
}
};
}
这是我从Logcat收集的内容
09-17 11:11:15.704: I/dalvikvm(1634): | sysTid=1634 nice=0 sched=0/0 cgrp=[fopen- error:2] handle=-1207757760
09-17 11:11:58.690: E/Trace(1665): error opening trace file: No such file or directory (2)
09-17 11:18:47.446: I/dalvikvm(1735): | sysTid=1735 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-1207757760
09-17 11:39:46.139: E/Trace(1758): error opening trace file: No such file or directory (2)
09-17 11:39:53.705: I/dalvikvm(1758): | sysTid=1758 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-1207757760
09-17 11:40:00.356: E/Trace(1774): error opening trace file: No such file or directory (2)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidsleepmachine.gamble"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.androidsleepmachine.gamble.Home"
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="com.androidsleepmachine.gamble.Ship" />
<activity android:name="com.androidsleepmachine.gamble.Ocean" />
</application>
</manifest>
答案 0 :(得分:0)
答案 1 :(得分:0)
您的代码完美无缺。我确实尝试在模拟器上运行它,它的工作非常好:
public class Ocean extends Activity实现了View.OnClickListener {
public static final Integer[] TIME_IN_MINUTES = { 30, 45, 60, 180, 360 };
public MediaPlayer mediaPlayer;
public Handler handler = new Handler();
public Button button1;
public Spinner spinner1;
// Initialize the activity
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.ocean);
button1 = (Button) findViewById(R.id.btn1);
button1.setOnClickListener(this);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
android.R.layout.simple_spinner_item, TIME_IN_MINUTES);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
}
// Play the sound and start the timer
private void playSound(int resourceId) {
// Cleanup any previous sound files
cleanup();
// Create a new media player instance and start it
mediaPlayer = MediaPlayer.create(this, resourceId);
mediaPlayer.start();
// Create the timer to stop the sound after x number of milliseconds
int selectedTime = TIME_IN_MINUTES[spinner1.getSelectedItemPosition()];
handler.postDelayed(runnable, selectedTime * 60 * 1000);
}
// Handle button callback
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
playSound(R.raw.test);
/*
* getResources().openRawResource(R.raw.test);
*/break;
}
}
// Stop the sound and cleanup the media player
public void cleanup() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
// Cancel any previously running tasks
handler.removeCallbacks(runnable);
}
// Runnable task used by the handler to stop the sound
public Runnable runnable = new Runnable() {
public void run() {
cleanup();
}
};
}
即使应用关闭,音乐也会继续播放。 我不认为代码有任何问题。