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>
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)