为了账号安全,请及时绑定邮箱和手机立即绑定

未找到上单击方法

未找到上单击方法

侃侃无极 2022-09-28 10:29:58
我是新手,我正在为一个大学项目做一个应用程序,我想在我的应用程序中放置一个VideoView,所以我看了这个视频(“https://www.youtube.com/watch?v=SrPHLj_q_OQ&t=421s”)并做了它并工作。但后来我添加将代码从content_main.xml复制到一个片段,它停止工作,并在按钮上的“android:onClick”上给出错误。当我按CTRL + F1进行检查时,它说:"Corresponding method handler'public void videoplay(android.view.View)' not foundInspection 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 "这是我的 xml:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="16dp"    android:paddingLeft="16dp"    android:paddingRight="16dp"    android:paddingTop="16dp"    android:background="#003e6f"    android:orientation="vertical"    tools:context=".InicioFragment">    <VideoView        android:id="@+id/videoView"        android:layout_width="match_parent"        android:layout_gravity="center_horizontal"        android:layout_height="197dp" />    <Button        android:id="@+id/button2"        style="@style/Widget.AppCompat.Button.Borderless"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:background="#FF6600"        android:text="Play"        android:onClick="videoplay"        android:textColor="#ffffff" />    <ImageView        android:id="@+id/imagem2"        android:layout_width="match_parent"        android:layout_height="360dp"        android:layout_alignParentBottom="true"        android:adjustViewBounds="false"        android:background="@drawable/tech3" /></LinearLayout>
查看完整描述

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.

            }

        });

    }

}


查看完整回答
反对 回复 2022-09-28
?
眼眸繁星

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();

            }

        });


查看完整回答
反对 回复 2022-09-28
?
慕容森

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){

          .........

          .....

    }


查看完整回答
反对 回复 2022-09-28
?
胡子哥哥

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");

            }

        });


查看完整回答
反对 回复 2022-09-28
  • 4 回答
  • 0 关注
  • 287 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号