when I click on the button the app crashes

EpicMario71

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

Zain

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

App crashes when I click the search button in my other fragments

App crashes when I click the button that opens a Popup window

Android app crashes when I click menu button

Countdowntimer cancel crashes the app when click on button

App Crashes when click back button in android

Android app crashes when click button

My Whole App Crashes When I Click My Play Again Button

How can I get the specific values I selected from SQLite Database? When I click on the button the app jut crashes

Android App crashes on Button click

The app is closed when I click the button

App crashes when I click a button. Log error - Caused by: java.lang.ArrayIndexOutOfBoundsException: length=9; index=9

My app crashes when i click on the Floating Action Button: Only the original thread that created a view hierarchy can touch its views

In Shiny/R when I click on image link (actionLink) the app crashes

My app crashes when I try to add Image Button to it

App crashes when I press "continue" button (transitioning between activities)

My Android App crashes when I hit the button

App crashes when I press the Up(back) button in settings menu

My app crashes when i press the start button

my app crashes "sometimes" when i press delete button

hi, i am new to android studio. I am trying to go to the next window, but when i click the button the app crashes. here is my code

Adding button click event crashes app at launch

App crashes on button Click Action iOS

Android App crashes upon menu button click

Android application crashes when trying to click button

Error when I click login button in react app

"App name" keeps stopping when I click the "TRANSLATE" button.

android app not closing when I click on home button

How to open Google Map when I click on a button in android app

redirect to the facebook app when I click share button in swift webview