【问题标题】:onClick: Method can not be found [duplicate]onClick:找不到方法[重复]
【发布时间】:2020-07-10 19:14:06
【问题描述】:

我对 Android Studio 很陌生。我想要的很简单:我按下一个按钮,应该会发生一些事情。

所以我的片段中有一个按钮,在 XML 中看起来像这样:

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:gravity="center_vertical"
    android:text="@string/hello"
    android:onClick="doSumth"
/>

我在 GeneralFragment.java 中的代码如下所示:

package ch.yourclick.kitt.fragments;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import ch.yourclick.kitt.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GeneralFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GeneralFragment extends Fragment {

    public GeneralFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment General.
     */
    // TODO: Rename and change types and number of parameters
    public static GeneralFragment newInstance() {
        GeneralFragment fragment = new GeneralFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_general, container, false);
    }

    public void doSumth() {
        System.out.println("Hello");
    }
}

我的问题是我得到了错误:

java.lang.IllegalStateException: 在视图类 androidx.appcompat.widget.App 上定义的 android:onClick 属性的父或祖先上下文中找不到方法 doSumth(View)

我做错了什么?

【问题讨论】:

    标签: java android android-fragments


    【解决方案1】:

    您必须向函数添加View 参数。它必须看起来像这样:

    public void doSumth(View view)
    {
        System.out.println("Hello");
    }
    

    Responding to Click Events - Android docs

    你也可以用这样的代码来制作它:

    public class MainActivity extends AppCompatActivity
    {
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button bt1 = (Button) findViewById(R.id.b1);
            bt1.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    System.out.println("Hello");
                }
            });
        }
    }
    

    【讨论】:

    • 我已经用那个参数试过了,但是一旦点击那个按钮,我得到了同样的错误。
    • @Reza 你确定你在 XML 文件中给出了正确的名称吗?如果你写了onClick 参数并且你没有得到提示你必须写错函数。您也可以检查我在编辑中添加的第二个选项
    • 是的,代码和我发布的一样。我已经尝试了您的第二个选项,但随后出现错误:无法在“GeneralFragment”中解析方法“setContentView”
    • 我已经在onCreateView() 中尝试了您的代码,而不是onCreate() 并且有效!谢谢!
    【解决方案2】:

    您需要在方法中添加一个 View 参数。当您在 XML 中使用 android:onClick 时,该方法需要一个类似视图的参数。 这段代码:

    public void doSumth(View view) {
        System.out.println("Hello");
    }
    

    【讨论】:

    • 我已经用那个参数试过了,但是一旦点击那个按钮,我得到了同样的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多