onClick: Method can not be found

Viewed 41

I am very new to Android Studio. What I want is simple: I press on a button and something should happen.

So I have a button in my fragment that looks in XML like this:

<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"
/>

My code in GeneralFragment.java looks like this:

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

My problem is that I get the error:

java.lang.IllegalStateException: Could not find method doSumth(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.App

What am I doing wrong?

2 Answers

You have to add a View argument to a function. It has to look like this:

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

Responding to Click Events - Android docs

Also You can make it in code like this:

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

You need add a View parameter in your method. When you use android:onClick in XML , the method need a view like parameter. The this code:

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