Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 点击按钮后应用程序停止工作_Android_Audio_Soundpool - Fatal编程技术网

Android 点击按钮后应用程序停止工作

Android 点击按钮后应用程序停止工作,android,audio,soundpool,Android,Audio,Soundpool,因此,我正在制作一个带有片段的应用程序,我想在点击按钮时听到声音。除了我点击按钮应用程序停止工作外,其他一切都正常。我在按钮上放了一个名为playAnother的onclick方法。我想问题可能在.java文件中:我不知道该做什么,我完全是初学者。祝你今天愉快 按钮的onClick是否已在fragment_three.xml布局中设置: <Button . . . android:onClick="playAnother" /> 方法播放另一个(视图)必须在您的活动中,而

因此,我正在制作一个带有片段的应用程序,我想在点击按钮时听到声音。除了我点击按钮应用程序停止工作外,其他一切都正常。我在按钮上放了一个名为playAnother的onclick方法。我想问题可能在.java文件中:我不知道该做什么,我完全是初学者。祝你今天愉快

按钮的onClick是否已在fragment_three.xml布局中设置:

<Button
 .
 .
 . 
 android:onClick="playAnother" />

方法
播放另一个(视图)
必须在您的活动中,而不是片段中

如果您想在片段中单击按钮,可以为按钮提供一个id:

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

关于标签的一点说明:JavaScript与此处的Javapost代码不同,而不是发布代码的图像!这应该对你有所帮助。Fragments无法引用layoutsso中的onClick事件我所要做的就是将FragmentThree.java中的所有内容放到MainActivity.java中,并在fragment_three.xml中保留按钮?也许这样可以,但拥有一个片段的目的是什么?最好处理片段中的点击。按照我在“或”下面的建议
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;
}