4 回答
TA贡献1846条经验 获得超7个赞
以下错误:
"Corresponding method handler'public void videoplay(android.view.View)' not found
Inspection info:The onClick attribute value should be the name of a method in this View's context to invoke when the view is clicked.This name must correspond to a public method that takes exactly one parameter of type View.
Must be a string value, using '\;' to escape characters such as '\n' or '\uxxxx' for a unicode character.
Issue id:OnClick "
表示在使用属性时忘记在 Activity 类中添加相应的方法。因此,您需要将以下方法添加到您的活动中:android:onClick="videoplay"
public void videoplay(View v){
// do something here.
}
最重要的部分是,android:onClick标签仅在您在活动的内容布局中使用它时才有效。因此,当您在片段布局中使用它时,它不起作用。您需要改用 。android:onClickonClickListener
您需要绑定视图并添加如下所示的内容:onClickListener
public class InicioFragment extends Fragment {
public InicioFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_inicio, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// bind the views here.
Button button2 = view.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something here.
}
});
}
}
TA贡献1873条经验 获得超9个赞
您可以将其用于单击。
Button mPlayVideo;
在创建中
mPlayVideo = findViewById(R.id.button2);
mPlayVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
});
TA贡献1853条经验 获得超18个赞
这是因为您在 .您应该将方法放入 中,因为它是您上面提到的布局的相应文件。videoplay()MainActivity.javavideoplay()InicioFragment.javaxml
初始化你的变量,如在你的方法中在 Inicio 碎片上onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_inicio, container, false);
Button btn1 = view.findViewById(R.id.button_id);
.....
.....
return view;
}
public void videoplay(View v){
.........
.....
}
TA贡献1825条经验 获得超6个赞
请注意,此文件的上下文是您的初始化碎片(工具:上下文=“。因此,视频播放(查看v)应该在您的“攻击”类中。这就是没有找到的原因。它不会搜索您的主要活动。
public class InicioFragment extends Fragment {
public InicioFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.testclassfragment, container, false);
Button clk = (Button) view.findViewById(R.id.button);
VideoView videov = (VideoView) view.findViewById(R.id.videoView);
return view;
}
public void videoplay(View v){
String videopath = "android.resource://intro.android.migueloliveiraapp/" + R.raw.oliveira;
Uri uri = Uri.parse(videopath);
videov.setVideoURI(uri);
videov.start();
}
}
由于您的按钮也有一个变量,因此另一个选项是执行以下操作:
clk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("TESTING", "Clicked");
}
});
添加回答
举报
