猿问

未找到上单击方法

我是新手,我正在为一个大学项目做一个应用程序,我想在我的应用程序中放置一个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 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 "

这是我的 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>


侃侃无极
浏览 288回答 4
4回答

喵喵时光机

以下错误:"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 "表示在使用属性时忘记在 Activity 类中添加相应的方法。因此,您需要将以下方法添加到您的活动中:android:onClick="videoplay"public void videoplay(View v){&nbsp; // do something here.}最重要的部分是,android:onClick标签仅在您在活动的内容布局中使用它时才有效。因此,当您在片段布局中使用它时,它不起作用。您需要改用 。android:onClickonClickListener您需要绑定视图并添加如下所示的内容:onClickListenerpublic class InicioFragment extends Fragment {&nbsp; &nbsp; public InicioFragment() {}&nbsp; &nbsp; @Override&nbsp; &nbsp; public View onCreateView(LayoutInflater inflater, ViewGroup container,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; return inflater.inflate(R.layout.fragment_inicio, container, false);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onViewCreated(View view, Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onViewCreated(view, savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; // bind the views here.&nbsp; &nbsp; &nbsp; &nbsp; Button button2 = view.findViewById(R.id.button2);&nbsp; &nbsp; &nbsp; &nbsp; button2.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// do something here.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}

眼眸繁星

您可以将其用于单击。Button mPlayVideo;在创建中mPlayVideo = findViewById(R.id.button2);mPlayVideo.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });

慕容森

这是因为您在 .您应该将方法放入 中,因为它是您上面提到的布局的相应文件。videoplay()MainActivity.javavideoplay()InicioFragment.javaxml初始化你的变量,如在你的方法中在 Inicio 碎片上onCreateView@Override&nbsp; &nbsp; public View onCreateView(LayoutInflater inflater, ViewGroup container,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; // Inflate the layout for this fragment&nbsp; &nbsp; &nbsp; &nbsp; View view = inflater.inflate(R.layout.fragment_inicio, container, false);&nbsp; &nbsp; &nbsp; &nbsp; Button btn1 = view.findViewById(R.id.button_id);&nbsp; &nbsp; &nbsp; &nbsp; .....&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.....&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }&nbsp; &nbsp; public void videoplay(View v){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .........&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .....&nbsp; &nbsp; }

胡子哥哥

请注意,此文件的上下文是您的初始化碎片(工具:上下文=“。因此,视频播放(查看v)应该在您的“攻击”类中。这就是没有找到的原因。它不会搜索您的主要活动。public class InicioFragment extends Fragment {&nbsp; &nbsp; public InicioFragment() {&nbsp; &nbsp; &nbsp; &nbsp; // Required empty public constructor&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public View onCreateView(LayoutInflater inflater, ViewGroup container,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; // Inflate the layout for this fragment&nbsp; &nbsp; &nbsp; &nbsp; View view = inflater.inflate(R.layout.testclassfragment, container, false);&nbsp; &nbsp; &nbsp; &nbsp; Button clk = (Button) view.findViewById(R.id.button);&nbsp; &nbsp; &nbsp; &nbsp; VideoView videov = (VideoView) view.findViewById(R.id.videoView);&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }&nbsp; &nbsp; public void videoplay(View v){&nbsp; &nbsp; &nbsp; &nbsp; String videopath = "android.resource://intro.android.migueloliveiraapp/" + R.raw.oliveira;&nbsp; &nbsp; &nbsp; &nbsp; Uri uri = Uri.parse(videopath);&nbsp; &nbsp; &nbsp; &nbsp; videov.setVideoURI(uri);&nbsp; &nbsp; &nbsp; &nbsp; videov.start();&nbsp; &nbsp; }}由于您的按钮也有一个变量,因此另一个选项是执行以下操作:clk.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.d("TESTING", "Clicked");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });
随时随地看视频慕课网APP

相关分类

Java
我要回答