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>
Aucun commentaire:
Enregistrer un commentaire