简体   繁体   中英

Android onClick inside Fragments not found

Hey I have a problem creating an animation inside of a fragment. I wanted to create a fadeIn animation with a "onClick", but when I click it, it crashes, and I don't understand why. I tried to create the Animation in a seperate XML file, but right now I it in the Fragment class.

Fragment XML file:

    <?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="#fff"
    tools:context=".SecondFragment">

    <ImageButton
        android:id="@+id/button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:contentDescription="Betätigungsknopf"
        android:onClick="onRotateButtonClicked"
        android:src="@drawable/ic_adjust_150"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:background="@drawable/my_border"
        android:text="Klicke um Suche zu starten."
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_down_50"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView7" />

</androidx.constraintlayout.widget.ConstraintLayout>

Fragment JAVA class:

    package com.example.appsplashscreen;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class SecondFragment extends Fragment {

    private ImageView i1;

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


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

    public void onRotateButtonClicked(View view) {
        // Tween Animation using Java code
        i1 = (ImageView) getView().findViewById(R.id.imageView4);
        Animation rotateAnimation = new RotateAnimation(
                0,
                360,
                Animation.RELATIVE_TO_SELF,
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f);
        rotateAnimation.setDuration(500);
        i1.startAnimation(rotateAnimation);
    }

    public void onStartSearchButtonClicked(View view) {

    }
}

Errormessage:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.appsplashscreen, PID: 25131 java.lang.IllegalStateException: Could not find method onRotateButtonClicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatImageButton with id 'button' at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:436) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393) at android.view.View.performClick(View.java:7125) at android.view.View.performClickInternal(View.java:7102) at ZC31B32364CE19CA8FCD1 50A417ECCE58Z.view.View.access$3500(View.java:801) at android.view.View$PerformClick.run(View.java:27336) 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)

If used Fragment then try below code. issue in your onRotateButtonClicked

public class SecondFragment extends Fragment implements OnClickListener{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_second, container, false);

        ImageButton button = (ImageButton) v.findViewById(R.id.button);
        button.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button :
                // your button click 
                i1 = (ImageView) getView().findViewById(R.id.imageView4);
                Animation rotateAnimation = new RotateAnimation(
                0,
                360,
                Animation.RELATIVE_TO_SELF,
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f);
                rotateAnimation.setDuration(500);
                i1.startAnimation(rotateAnimation);
            break;
        }
    }
}

its more better that just inflate layout in

onCreateView()

and you should implement your logic in

onViewCreated()

    View rootView; 
    Animation rotateAnimation;
    Button btn;

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


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

       private void setupViews(){
       i1 = rootView.findViewById(R.id.imageView4);
       btn=rootView.findViewByIdR.id.youtButton)
       rotateAnimation = new RotateAnimation(
                    0,
                    360,
                    Animation.RELATIVE_TO_SELF,
                    0.5f,
                    Animation.RELATIVE_TO_SELF,
                    0.5f);

           rotateAnimation.setDuration(500);

          btn.setOnClickListener(new View.OnClickListener() {
             @Override
            public void onClick(View v) {
              onRotateButtonClicked();
        }
    });

    }

        public void onRotateButtonClicked() { i1.startAnimation(rotateAnimation)}

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM