-1

I'm using Android Studio 3.5 and this issue appear when use it in fragment .. I'm try to use checkbox on fragment but when checked when run on mobile (the application close and give error ) i don't know from where the problem is .

Anyone can help me to solve this issue .. thanks note : I built bottom navigation and this fragment that show on fragment not in activity . thanks :

othersFragment:

public class othersActivity extends Fragment {

ArrayList<Integer> selection = new ArrayList<Integer>();

CheckBox send_it ;
Button btn_totalPrice , btn_submit;
TextView total_price;


public othersActivity (){

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_others, container, false);

//ini checkBox
send_it = v.findViewById(R.id.ch_sendITE);

//ini btn (submit & total price) and Textview
btn_totalPrice = v.findViewById(R.id.btn_submit);
btn_submit = v.findViewById(R.id.btn_totalPrice);

total_price = v.findViewById(R.id.t_totalPrice);

return v;
}


public void selectedItem(View view){
boolean checked = ((CheckBox) view).isChecked();

switch (view.getId()){
        case R.id.ch_sendITE:
            if(checked){
                selection.add(5);
            }else{
                selection.remove(Integer.valueOf("5"));
            }
            break;

    }

}

public void final_price(View v){

    String final_price  = "";
    double sum = 0;
    for(int i=0;i<selection.size(); i++){
        sum = sum + selection.get(i);
    }
    String res = String.valueOf(sum);
    final_price = res;
    total_price.setText(final_price);

}

}

Activity_others.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".fragments.othersActivity">

<RelativeLayout
    android:id="@+id/banner"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@drawable/banner3">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:fontFamily="casual"
        android:text="e - Request"
        android:textColor="@color/colorDarkBlue"
        android:textSize="50dp"
        android:textStyle="bold" />

</RelativeLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/banner"
    android:orientation="vertical"
    android:paddingLeft="20dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="20dp"
        android:text="Please select your item:"
        android:textColor="@color/colorBlack"
        android:textSize="30dp"
        android:textStyle="bold" />

    <CheckBox
        android:onClick="selectedItem"
        android:id="@+id/ch_sendITE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/send_ITE"
        android:textSize="20dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/show_totalPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Total price : "
            android:textSize="20dp" />

        <TextView
            android:id="@+id/t_totalPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" 0.0 "
            android:textSize="20dp" />

        <TextView
            android:id="@+id/K.d_totalPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" K.D "
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="25dp"
        android:orientation="horizontal">

        <Button
            android:onClick="final_price"
            android:id="@+id/btn_totalPrice"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_log_regist"
            android:text="Total price"
            android:textColor="@color/colorWhite" />

        <Button
            android:id="@+id/btn_submit"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:background="@drawable/btn_log_regist"
            android:text="Submit"
            android:textColor="@color/colorWhite" />

    </LinearLayout>


</LinearLayout>

</RelativeLayout>

method never is used

i used this and same issue ... once checked the box the app close

v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectedItem(view);
            final_price(view);
        }
});

this is the Logcat :

2020-03-28 07:43:47.258 31000-31000/com.example.itspt D/AndroidRuntime: Shutting down VM
2020-03-28 07:43:47.260 31000-31000/com.example.itspt E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.itspt, PID: 31000
    java.lang.IllegalStateException: Could not find method 11(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatCheckBox with id 'ch_sendITE'
        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: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)
Zain
  • 37,492
  • 7
  • 60
  • 84
M.alsaoud
  • 23
  • 4

1 Answers1

0

So you get this error:

java.lang.IllegalStateException: Could not find method 11(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatCheckBox with id 'ch_sendITE'

And the reason is that it can't find the method you defined in the andorid:onClick attribute for your CheckBox which you named as selectedItem(View view), however you already put it on your fragment! so what's going on ?

So basically, you can't use andorid:onClick attribute in fragment layouts the same way as you use in the activities' layouts. The reason you got this exception is that the android system can't find the selectedItem(View view) in your activity; because you defined it in your fragment rather that your activity.

That is clear in the docs that android:onClick attribute should only be used in activity layouts as stated in here "Within the Activity that hosts this layout, the following method handles the click event".

So to solve this you have a couple of options:

Option 1: Handle fragment view's click handlers programmatically with OnClickListener


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v= inflater.inflate(R.layout.activity_others, container, false);

   //init check Box
    send_it = v.findViewById(R.id.ch_sendITE);

    send_it.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

return v;

}

Option 2: If you insist on using android:onClick on your fragment's layout, then you should put the selectedItem() method in your activity as well, and then explicitly call the selectedItem() of your fragment from the activity's selectedItem():

In your activity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.temp_activity);

    }
    public void selectedItem(View view) {
        ((MyFragmentClass) getSupportFragmentManager().findFragmentById(R.id.fragment_container)).selectedItem(view);
    }

}

Activity layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/fragment_container"
        android:name="com.example.android.myapp.MyFragmentClass"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

Check out these: 1, 2 for more details.

Zain
  • 37,492
  • 7
  • 60
  • 84