我是编程新手,我正在使用eclipse进行编程。
我正在创建一个简单的音板,但是我收到警告“未使用局部变量SoundButton38的值”,但我想使用该变量。
SoundButton1-37正在运行且没有错误,但38-63确实有警告。我不确定是什么导致了它以及它为什么会发生。
这是我的代码片段,警告即将出现(35,36和37没有警告:
Button SoundButton35 = (Button)findViewById(R.id.sound35);
SoundButton35.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(35);
}
});
Button SoundButton36 = (Button)findViewById(R.id.sound36);
SoundButton36.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(36);
}
});
Button SoundButton37 = (Button)findViewById(R.id.sound37);
SoundButton35.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(37);
}
});
Button SoundButton38 = (Button)findViewById(R.id.sound38);
SoundButton35.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(38);
}
});
这是我的代码的开头
package com.soundboardtest;
import com.soundboardtest.R;
import com.soundboardtest.SoundManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Soundboard extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soundboardtest);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.sound1);
mSoundManager.addSound(2, R.raw.sound2);
mSoundManager.addSound(3, R.raw.sound3);
mSoundManager.addSound(4, R.raw.sound4);
mSoundManager.addSound(5, R.raw.sound5);
mSoundManager.addSound(6, R.raw.sound6);
mSoundManager.addSound(7, R.raw.sound7);
mSoundManager.addSound(8, R.raw.sound8);
mSoundManager.addSound(9, R.raw.sound9);
mSoundManager.addSound(10, R.raw.sound10);
mSoundManager.addSound(11, R.raw.sound11);
mSoundManager.addSound(12, R.raw.sound12);
mSoundManager.addSound(13, R.raw.sound13);
mSoundManager.addSound(14, R.raw.sound14);
mSoundManager.addSound(15, R.raw.sound15);
mSoundManager.addSound(16, R.raw.sound16);
mSoundManager.addSound(17, R.raw.sound17);
mSoundManager.addSound(18, R.raw.sound18);
mSoundManager.addSound(19, R.raw.sound19);
mSoundManager.addSound(20, R.raw.sound20);
mSoundManager.addSound(21, R.raw.sound21);
mSoundManager.addSound(22, R.raw.sound22);
mSoundManager.addSound(23, R.raw.sound23);
mSoundManager.addSound(24, R.raw.sound24);
mSoundManager.addSound(25, R.raw.sound25);
mSoundManager.addSound(26, R.raw.sound26);
mSoundManager.addSound(27, R.raw.sound27);
mSoundManager.addSound(28, R.raw.sound28);
mSoundManager.addSound(29, R.raw.sound29);
mSoundManager.addSound(30, R.raw.sound30);
mSoundManager.addSound(31, R.raw.sound31);
mSoundManager.addSound(32, R.raw.sound32);
mSoundManager.addSound(33, R.raw.sound33);
mSoundManager.addSound(34, R.raw.sound34);
mSoundManager.addSound(35, R.raw.sound35);
mSoundManager.addSound(36, R.raw.sound36);
mSoundManager.addSound(37, R.raw.sound37);
mSoundManager.addSound(38, R.raw.sound38);
mSoundManager.addSound(39, R.raw.sound39);
mSoundManager.addSound(40, R.raw.sound40);
mSoundManager.addSound(41, R.raw.sound41);
mSoundManager.addSound(42, R.raw.sound42);
mSoundManager.addSound(43, R.raw.sound43);
mSoundManager.addSound(44, R.raw.sound44);
mSoundManager.addSound(45, R.raw.sound45);
mSoundManager.addSound(46, R.raw.sound46);
mSoundManager.addSound(47, R.raw.sound47);
mSoundManager.addSound(48, R.raw.sound48);
mSoundManager.addSound(49, R.raw.sound49);
mSoundManager.addSound(50, R.raw.sound50);
mSoundManager.addSound(51, R.raw.sound51);
mSoundManager.addSound(52, R.raw.sound52);
mSoundManager.addSound(53, R.raw.sound53);
mSoundManager.addSound(54, R.raw.sound54);
mSoundManager.addSound(55, R.raw.sound55);
mSoundManager.addSound(56, R.raw.sound56);
mSoundManager.addSound(57, R.raw.sound57);
mSoundManager.addSound(58, R.raw.sound58);
mSoundManager.addSound(59, R.raw.sound59);
mSoundManager.addSound(60, R.raw.sound60);
mSoundManager.addSound(61, R.raw.sound61);
mSoundManager.addSound(62, R.raw.sound62);
mSoundManager.addSound(63, R.raw.sound63);
感谢您的帮助,我无法为此找到一个好的答案。
答案 0 :(得分:1)
Button SoundButton38 = (Button)findViewById(R.id.sound38);
SoundButton35.setOnClickListener(new OnClickListener() {
在这里看起来像一个错字,我认为应该是
SoundButton38.setOnClickListener(new OnClickListener() {
Similiarly
Button SoundButton37 = (Button)findViewById(R.id.sound37);
SoundButton35.setOnClickListener(new OnClickListener() {
应该是
SoundButton37.setOnClickListener(new OnClickListener() {
答案 1 :(得分:0)
您只声明并初始化它,而不是在
的任何地方使用它Button SoundButton38 = (Button)findViewById(R.id.sound38);
你可能想要
SoundButton38.setOnClickListener(new OnClickListener() { // SoundButton38 not 35
public void onClick(View v) {
mSoundManager.playSound(38);
}
});
类似地
SoundButton37.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(37);
}
});