安卓日食开关盒不工作



到目前为止,我有一个应用程序,很多用户都帮助了我,我真的很感激。现在我有一个强制关闭问题,当案件在下面被处理时。当我单击按钮 2 时,它将加载 Ship.Class,并且一切都像我想要的那样工作。当我单击按钮1时,它不会加载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>

你的代码工作得很好。我确实尝试在模拟器上运行它,它运行良好:

public class Ocean extend 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();
    }
};

}

即使应用程序关闭,音乐也会继续播放。我认为代码没有任何问题。

  1. 检查您是否在安卓清单文件中添加了海洋类(活动)。
  2. 检查布局中的 id 是否与 button1 相同。

最新更新