Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] Accessing onClick form an Android fragment using Navigation

Posted on November 11, 2022 By

Solution 1 :

The problem is you’re doing both (both are incomplete though…), implementing the OnClickListener and have defined a method in the xml. Do any one of them.

I’ll specify both of those, to do it only in the xml you’ll have to specify your onClick() method in the xml not

<Button
    android_id="@+id/oBlocationButton"
    ....
    android_onClick="onClick"/>

In your fragment just create this method: (do not implement the listener)

public class OBWelcomeFragment extends Fragment {

    ....

    // notice -> no override here..
    public void onClick(View v) {


    }
}

Or the other way: this time don’t specify onClick in xml, instead just find button and set listener.

public class OBWelcomeFragment extends Fragment implements View.OnClickListener {

    .....

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Button btn = view.findViewById(R.id.oBlocationButton);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        NavDirections action = OBWelcomeFragmentDirections.actionOBWelcomeFragmentToOBLocationFragment3();
        Navigation.findNavController(v).navigate(action);
    }
}

Problem :

I cannot set an onClick listener fo my button in a fragment. Maybe i have the context wrong but i cannot see it.

Please help below i think i am doing everything Ok? I though i understood the context working but it seems to me that what works one day does not the next?

fragment xml:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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="@color/colorPrimary"
    tools_context=".OBWelcomeFragment">

    <ImageView
        android_id="@+id/imageView3"
        android_layout_width="150dp"
        android_layout_height="150dp"
        android_src="@drawable/logo_main"
        app_layout_constraintBottom_toBottomOf="parent"
        app_layout_constraintEnd_toEndOf="parent"
        app_layout_constraintHorizontal_bias="0.498"
        app_layout_constraintStart_toStartOf="parent"
        app_layout_constraintTop_toTopOf="parent"
        app_layout_constraintVertical_bias="0.08" />

    <ScrollView
        android_id="@+id/scrollView3"
        android_layout_width="324dp"
        android_layout_height="333dp"
        app_layout_constraintBottom_toBottomOf="parent"
        app_layout_constraintEnd_toEndOf="parent"
        app_layout_constraintHorizontal_bias="0.494"
        app_layout_constraintStart_toStartOf="parent"
        app_layout_constraintTop_toBottomOf="@+id/imageView3"
        app_layout_constraintVertical_bias="0.594">

        <TextView
            android_id="@+id/tv_description_heading"
            android_layout_width="match_parent"
            android_layout_height="match_parent"
            android_text="@string/ob_welcome_text"
            android_textColor="@color/cardview_light_background"
            android_textSize="18sp" />

    </ScrollView>

    <TextView
        android_id="@+id/textView3"
        android_layout_width="wrap_content"
        android_layout_height="wrap_content"
        android_layout_marginTop="32dp"
        android_text="@string/app"
        android_textColor="@color/cardview_light_background"
        android_textSize="36sp"
        app_layout_constraintBottom_toTopOf="@+id/scrollView3"
        app_layout_constraintEnd_toEndOf="parent"
        app_layout_constraintStart_toStartOf="parent"
        app_layout_constraintTop_toBottomOf="@+id/imageView3"
        app_layout_constraintVertical_bias="0.18" />

    <Button
        android_id="@+id/oBlocationButton"
        android_layout_width="0dp"
        android_layout_height="wrap_content"
        android_layout_marginStart="32dp"
        android_layout_marginLeft="32dp"
        android_layout_marginEnd="32dp"
        android_layout_marginRight="32dp"
        android_layout_marginBottom="32dp"
        android_onClick="onClick"
        android_text="Next"
        app_layout_constraintBottom_toBottomOf="parent"
        app_layout_constraintEnd_toEndOf="parent"
        app_layout_constraintStart_toStartOf="parent"
        app_layout_constraintTop_toBottomOf="@+id/scrollView2" />
    </androidx.constraintlayout.widget.ConstraintLayout>

The corresponding fragment

    import android.os.Bundle;

    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentActivity;
    import androidx.navigation.NavDirections;
    import androidx.navigation.Navigation;

    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;


    public class OBWelcomeFragment extends Fragment implements View.OnClickListener {

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

    public static OBWelcomeFragment newInstance(String param1, String param2) {
    OBWelcomeFragment fragment = new OBWelcomeFragment();
    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_o_b_welcome, container, false);
    }

    @Override
    public void onClick(View v) {

        NavDirections action = 
    OBWelcomeFragmentDirections.actionOBWelcomeFragmentToOBLocationFragment3();
        Navigation.findNavController(v).navigate(action);

    }
    }

The error

    2020-02-27 12:43:50.375 15523-15523/com.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app, PID: 15523
    java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor 
    Context for android:onClick attribute defined on view class 
    androidx.appcompat.widget.AppCompatButton with id 'oBlocationButton'
        at 
androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatView 
    Inflater.java:436)
        at 
androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflat .er.java:393)
        at android.view.View.performClick(View.java:7259)
        at android.view.View.performClickInternal(View.java:7236)
        at android.view.View.access$3600(View.java:801)
        at android.view.View$PerformClick.run(View.java:27892)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Please let me know if you need anything else?

READ  [FIXED] mobile - Android Deleted File Search
Powered by Inline Related Posts

Comments

Comment posted by docker dev

Thank you your advice really helped.

Android Tags:android, android-fragments, android-navigation

Post navigation

Previous Post: [FIXED] Xamarin.forms – Trouble behaviour between ListView & keyboard
Next Post: [FIXED] android – two elements in linear layout are packed to center instead of to the sides

Related Posts

[FIXED] android – Glide – ImageView height is 0dp for first load Android
[FIXED] android – Recycler view skipping layout Android
[FIXED] java – MPAndroidChart customizing horizontal BarChart Android
[FIXED] android – react native firebase handle click action on push notification Android
[FIXED] android – Unresolved reference: createNotificationChannel Android
[FIXED] c# – Collection View Items can’t Bind on FileInfo type. [Xamarin Forms] Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme