Skip to content
Advertisement

when I click on the button the app crashes

Here the error:

java.lang.IllegalStateException: Could not find method OpenRadio(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageButton with id 'RadioPootis'

java code:

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

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Fragment1.
     */
    // TODO: Rename and change types and number of parameters
    public static Fragment1 newInstance(String param1, String param2) {
        Fragment1 fragment = new Fragment1();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

    String url = "https://epicmario71.tk";

    public void OpenRadio(View view) {
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
        CustomTabsIntent customTabsIntent = builder.build();
        customTabsIntent.launchUrl(getActivity(), Uri.parse(url));
    }
}

xml file:

?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/background"
    tools:context=".ui.main.Fragment1">

    <ImageView
        android:id="@+id/imageView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="18dp"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="15dp"
        android:src="@drawable/webp_net_resizeimage__6_"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="22dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="15dp"
        android:src="@drawable/webp_net_resizeimage__6_"
        app:layout_constraintStart_toEndOf="@+id/RadioPootis"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="22dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="15dp"
        android:src="@drawable/webp_net_resizeimage__6_"
        app:layout_constraintStart_toEndOf="@+id/imageView12"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/RadioPootis"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="22dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="16dp"
        android:background="@null"
        android:onClick="OpenRadio"
        android:src="@drawable/webp_net_resizeimage__6_"
        app:layout_constraintStart_toEndOf="@+id/imageView11"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

when I click on the button the app crashes

Using tabbed activity from android studio example whit legacy android studio libraries

Any help is welcome

Advertisement

Answer

You can not use android:onClick attribute in fragment layout unless you create it in the activity class, not the fragment class.

In your case:

Here the error: java.lang.IllegalStateException: Could not find method OpenRadio(View) in a parent or ancestor Context for android:onClick attribute defined on view class

The system looks at the activity class that hosts Fragment1, and didn’t find OpenRadio(View) method.

So, you need to either move the OpenRadio(View) method to the activity class in case the code it holds is not relevant to the fragment; or you need to build some communication between the fragment and the activity when OpenRadio(View) of the activity is called, for this part you can check answers here.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement