发现我的错误。我的 muteSound 方法缺少 View 参数。
我正在创建一个应用程序来播放像inudge.net这样的音乐。我已经能够遍历列并播放声音并实现播放/暂停功能。但是,如果我在线程运行时尝试使用任何其他 UI 组件(例如按钮),则应用程序会崩溃。例如:当我按下静音按钮并调用 muteSound 方法时,应用程序停止工作并崩溃。该线程位于 playSound() 方法中。
package com.example.tunegrid;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private SoundPool soundPool;
private AudioManager audioManager;
boolean loadComplete = false;
private GridButton grid[][];
Button btnPlay;
boolean pause = true;
private int tempo = 1000;
private int r = -1;
private Integer sound[][];
private TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(100, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
loadComplete = true;
txt.setText("SoundPool load complete");
}
});
txt = (TextView) findViewById(R.id.textView1);
btnPlay = (Button) findViewById(R.id.btnPlay);
createDummyGrid();
}
public void onClick(View v)
{
Button btn = (Button)v;
int id = btn.getId();
int r = id / 4;
int c = id - 4 * r;
grid[r][c].toggleEnable();
}
public void playSound(View v)
{
if(pause) { pause = false; btnPlay.setText("Pause"); }
else { pause = true; btnPlay.setText("Play"); }
final Handler handler = new Handler();
Thread thread = new Thread()
{
public void run()
{
while (true)
{
if (pause) { break; }
handler.post(new Runnable() {
public void run()
{
int col = getNextCol();
sequence(col);
txt.setText("Playing column - " + col);
}
});
try { sleep(tempo); } catch (Exception e) {}
}
}
};
thread.start();
}
public void muteSound()
{
}
private int getNextCol() {
if (r == 3) { r = 0; }
else { r++; }
return r;
}
private void sequence(int r)
{
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float streamVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
for (int c=0; c < 4; c++)
{
if (grid[c][r].isEnabled())
{
soundPool.play(sound[c][r], streamVolume, streamVolume, 1, 0, 1f);
}
}
}
private void createDummyGrid() {
grid = new GridButton[4][4];
sound = new Integer[4][4];
for (int r=0; r < 4; r++)
{
for (int c=0; c < 4; c++)
{
grid[r][c] = new GridButton(this);
grid[r][c].setId(4*r+c);
// grid[r][c].setOnClickListener((OnClickListener) this);
if (c == 0) { sound[r][c] = soundPool.load(this, R.raw.s1, 1); }
else if (c == 1) { sound[r][c] = soundPool.load(this, R.raw.s2, 1); }
else if (c == 2) { sound[r][c] = soundPool.load(this, R.raw.s3, 1); }
else if (c == 3) { sound[r][c] = soundPool.load(this, R.raw.s4, 1); }
}
}
grid[0][0].toggleEnable();
grid[1][0].toggleEnable();
grid[2][0].toggleEnable();
grid[1][1].toggleEnable();
grid[2][2].toggleEnable();
grid[3][3].toggleEnable();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
日志猫
02-16 04:51:32.547: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.547: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.626: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.636: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.649: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.649: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.656: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.666: W/Trace(851): Unexpected value from nativeGetEnabledTags: 0
02-16 04:51:32.676: D/AndroidRuntime(851): Shutting down VM
02-16 04:51:32.676: W/dalvikvm(851): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-16 04:51:32.696: E/AndroidRuntime(851): FATAL EXCEPTION: main
02-16 04:51:32.696: E/AndroidRuntime(851): java.lang.IllegalStateException: Could not find a method muteSound(View) in the activity class com.example.tunegrid.MainActivity for onClick handler on view class android.widget.Button with id 'btnMute'
02-16 04:51:32.696: E/AndroidRuntime(851): at android.view.View$1.onClick(View.java:3584)
02-16 04:51:32.696: E/AndroidRuntime(851): at android.view.View.performClick(View.java:4202)
02-16 04:51:32.696: E/AndroidRuntime(851): at android.view.View$PerformClick.run(View.java:17340)
02-16 04:51:32.696: E/AndroidRuntime(851): at android.os.Handler.handleCallback(Handler.java:725)
02-16 04:51:32.696: E/AndroidRuntime(851): at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 04:51:32.696: E/AndroidRuntime(851): at android.os.Looper.loop(Looper.java:137)
02-16 04:51:32.696: E/AndroidRuntime(851): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-16 04:51:32.696: E/AndroidRuntime(851): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 04:51:32.696: E/AndroidRuntime(851): at java.lang.reflect.Method.invoke(Method.java:511)
02-16 04:51:32.696: E/AndroidRuntime(851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-16 04:51:32.696: E/AndroidRuntime(851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-16 04:51:32.696: E/AndroidRuntime(851): at dalvik.system.NativeStart.main(Native Method)
02-16 04:51:32.696: E/AndroidRuntime(851): Caused by: java.lang.NoSuchMethodException: muteSound [class android.view.View]
02-16 04:51:32.696: E/AndroidRuntime(851): at java.lang.Class.getConstructorOrMethod(Class.java:460)
02-16 04:51:32.696: E/AndroidRuntime(851): at java.lang.Class.getMethod(Class.java:915)
02-16 04:51:32.696: E/AndroidRuntime(851): at android.view.View$1.onClick(View.java:3577)
02-16 04:51:32.696: E/AndroidRuntime(851): ... 11 more
02-16 04:51:35.496: I/Process(851): Sending signal. PID: 851 SIG: 9