2

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?

xRay
  • 543
  • 1
  • 5
  • 29

2 Answers2

2

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");
            }
        });
    }
}
iknow
  • 8,358
  • 12
  • 41
  • 68
  • I have tried it with that parameter but once clicking on that button, I get the same error. – xRay Jul 10 '20 at 19:24
  • @Reza are You sure You gave the correct name in XML file? If You write `onClick` parameter and You don't get prompt You have to write the wrong function. Also You can check the second option which I added in edit – iknow Jul 10 '20 at 19:30
  • Yes, the code is just the same as I have posted. I have tried out your second option but then I get the error: Cannot resolve method 'setContentView' in 'GeneralFragment' – xRay Jul 10 '20 at 19:43
  • 1
    I have tried your code in `onCreateView()`, instead of `onCreate()` and that works! Thank you! – xRay Jul 10 '20 at 19:50
0

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");
}
Felipe Palma
  • 378
  • 1
  • 9
  • I have tried it with that parameter but once clicking on that button, I get the same error. – xRay Jul 10 '20 at 19:25