亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

未找到上單擊方法

未找到上單擊方法

侃侃無極 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 關注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號