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?