因此,我正在制作带有片段的应用程序,并且希望在单击按钮时发出声音。一切正常,但当我单击按钮时,该应用程序停止工作。我在按钮上放置了一个名为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/