Android Fragments 复选框onClick xml方法调用不起作用(未解析的方法)

pu82cl6c  于 2023-04-21  发布在  Android
关注(0)|答案(1)|浏览(168)

I have a map fragment inside a Drawer activity, I just added a checkbox to the mapFragment layout like so:

<?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:id="@+id/map_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".mapFragment">

<!-- TODO: Update blank fragment layout -->
<fragment
    android:id="@+id/map1"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"

    android:layout_alignParentStart="true"
    android:tag="home"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:context="com.tesseract.psiclops.zero.Main" />

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="87dp"
    android:background="@drawable/edittext_designtst"
    android:ems="10"
    android:hint=" ■ ¿Qué buscas hoy?"
    android:inputType="textPersonName"
    android:textColor="#806b63"
    android:textColorHint="#806b63"
    android:textColorLink="@color/colorPrimary"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<CheckBox
    android:id="@+id/follow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:checked="true"
    android:onClick="followIO"
    android:text="Sígueme"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

I have specified the onClick method "followIO" to trigger everytime the checkbox its checked/unchecked so the user can activate/deactivate camera updates of the map kind of like a "Follow me" checkbox.
I've declared the method inside the map fragment code since its the related activity(right?)

public class mapFragment extends Fragment implements OnMapReadyCallback, LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
.
.
.
public void followIO(View view){

    boolean checked = ((CheckBox)view).isChecked();

    if(checked){
    //Updates on
    }else{
   //Updates off
    }

} 
}

but I still get the warning: "Cannot Resolve symbol followIO".
When I try to uncheck the checkbox the app crashes and throws this exception:

  • E/AndroidRuntime: FATAL EXCEPTION: main Process: com.tesseract.psiclops.zerov2, PID: 5473 java.lang.IllegalStateException: Could not find method followIO(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatCheckBox with id 'follow' at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) at android.view.View.performClick(View.java:6256) at android.widget.CompoundButton.performClick(CompoundButton.java:134) at android.view.View$PerformClick.run(View.java:24701) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)*

I'm still pretty new at android and I couldn't find other solution to my problem so I hope someone can help, Thank you in advance!

ztmd8pv5

ztmd8pv51#

我刚刚找到了答案,我会把它贴在这里,以防有人发现它有用:
显然,xml方法声明不能很好地与片段一起工作,最好的方法是将变量与复选框关联,然后调用onCLicklistener。然后我从那里调用方法如下:

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

   follow=view.findViewById(R.id.follow);

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

相关问题