为什么不能在 btn.setonclicklistener 方法中调用方法
why can't call a method inside the btn.setonclicklistener method
当我尝试在 setonclicklistener 中调用方法 loadSound 时,声音没有出现,但是当我在 onCreateView 或 onStart 方法中调用它时,声音出现并且一切正常。
有什么问题 ?
有没有办法只在按下按钮时调用方法??
甚至我在 setonclicklistener 方法中调用了 setOnLoadCompleteListener 方法,但仍然没有帮助。
public class InsectsFragment extends Fragment {
private View view;
private int mBeeSound;
public InsectsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_insects, container, false);
Sound.mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
Sound.loaded = true;
}
});
ImageButton beeBtn = (ImageButton) view.findViewById(R.id.imageButtonBee);
beeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Sound.mStreamID > 0) {
Sound.mSoundPool.stop(Sound.mStreamID);
}
mBeeSound = Sound.loadSound(getContext(), "bee.mp3");
if (Sound.loaded) {
Sound.playSound(mBeeSound);
Snackbar.make(view, R.string.Bee, Snackbar.LENGTH_SHORT).show();
Toast.makeText(view.getContext(), R.string.Bee, Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_insects, container, false);
ImageButton beeBtn = (ImageButton) view.findViewById(R.id.imageButtonBee);
beeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Sound.mStreamID > 0) {
Sound.mSoundPool.stop(Sound.mStreamID);
}
Sound.mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
Sound.loaded = true;
if (Sound.loaded) {
Sound.playSound(mBeeSound);
Snackbar.make(view, R.string.Bee, Snackbar.LENGTH_SHORT).show();
Toast.makeText(view.getContext(), R.string.Bee, Toast.LENGTH_SHORT).show();
}
}
});
mBeeSound = Sound.loadSound(getContext(), "bee.mp3");
}
});
return view;
}
确保将正确的上下文传递给 soundpool.Inside setclicklistener 可能是您正在传递侦听器的上下文。
当我尝试在 setonclicklistener 中调用方法 loadSound 时,声音没有出现,但是当我在 onCreateView 或 onStart 方法中调用它时,声音出现并且一切正常。 有什么问题 ? 有没有办法只在按下按钮时调用方法??
甚至我在 setonclicklistener 方法中调用了 setOnLoadCompleteListener 方法,但仍然没有帮助。
public class InsectsFragment extends Fragment {
private View view;
private int mBeeSound;
public InsectsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_insects, container, false);
Sound.mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
Sound.loaded = true;
}
});
ImageButton beeBtn = (ImageButton) view.findViewById(R.id.imageButtonBee);
beeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Sound.mStreamID > 0) {
Sound.mSoundPool.stop(Sound.mStreamID);
}
mBeeSound = Sound.loadSound(getContext(), "bee.mp3");
if (Sound.loaded) {
Sound.playSound(mBeeSound);
Snackbar.make(view, R.string.Bee, Snackbar.LENGTH_SHORT).show();
Toast.makeText(view.getContext(), R.string.Bee, Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_insects, container, false);
ImageButton beeBtn = (ImageButton) view.findViewById(R.id.imageButtonBee);
beeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Sound.mStreamID > 0) {
Sound.mSoundPool.stop(Sound.mStreamID);
}
Sound.mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
Sound.loaded = true;
if (Sound.loaded) {
Sound.playSound(mBeeSound);
Snackbar.make(view, R.string.Bee, Snackbar.LENGTH_SHORT).show();
Toast.makeText(view.getContext(), R.string.Bee, Toast.LENGTH_SHORT).show();
}
}
});
mBeeSound = Sound.loadSound(getContext(), "bee.mp3");
}
});
return view;
}
确保将正确的上下文传递给 soundpool.Inside setclicklistener 可能是您正在传递侦听器的上下文。