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");
}
});
添加回答
舉報