android - 单击按钮后,应用停止运行

标签 android audio soundpool

因此,我正在制作带有片段的应用程序,并且希望在单击按钮时发出声音。一切正常,但当我单击按钮时,该应用程序停止工作。我在按钮上放置了一个名为playAnother的onclick方法。我认为问题可能在.java文件中:我不知道该怎么办。祝你今天愉快!

最佳答案

playAnother(View view)方法必须在您的Activity中,而不是片段中。

要么

如果要处理按钮,请在片段中单击,然后为按钮指定一个ID:

<Button
  android:id="@+id/button"
  .
  .

然后在你的片段onCreateView()
public View onCreateView(...) {
    // First we save the reference to the views of your fragment
    View view = inflater.inflate(R.layout.fragment_three, container, false);

    // Then we need to find the button-view
    Button button = (Button) view.findViewById(R.id.button);
    // And finally we can register OnClickListener for the button
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Handle your button click here
        }
    });
    return view;
}

关于android - 单击按钮后,应用停止运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43020998/

相关文章:

android - 底部对话框打开时如何防止键盘关闭?

android - 如果 HTTP 或 HTTPS 不包含在给定的 URL 中,如何在 android Web View 中加载 URL

web-applications - 您如何识别网络应用中的语音

android - AudioTrack : AudioFlinger could not create track, 状态:-32

Android Soundpool Load(String path, int priority)

android - Android停止跨多个 Activity 的SoundPool循环。或在更改页面时停止所有播放声音

android - 应用程序关闭时保持用户登录 - Android Studio - MySQL

android - GCM 无法在实时服务器上运行

objective-c - 播放按钮并暂停背景音

iphone - 如何在iPhone上播放音调