'App crashes after adding java code to Bottom Navigation home fragment
App was working perfectly before adding the java code(where i am trying to create a BMI calculator) it entered the bottom navigation but now it crashes. Here is the java code: HomeFragment.java
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.example.myapplication.BmiActivity;
import com.example.myapplication.BotnaV;
import com.example.myapplication.R;
import com.example.myapplication.databinding.FragmentHomeBinding;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.w3c.dom.Text;
import java.text.DecimalFormat;
public class HomeFragment extends Fragment implements View.OnClickListener {
private FragmentHomeBinding binding;
CardView weightCardView;
CardView ageCardView;
TextView weightCounterText, ageCounterText, height_title_text;
FloatingActionButton weightBtnInc, ageBtnInc;
FloatingActionButton weightBtnDec, ageBtnDec;
int weightCounter = 50;
int ageCounter = 25;
String countWeight, countAge;
NumberPicker feetPicker, inchPicker;
int feetValue = 5 , inchValue = 4;
Button calculateBtn;
String heightValue;
DecimalFormat decimalFormat;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
weightCardView = getActivity().findViewById(R.id.weight_cardView);
ageCardView = getActivity().findViewById(R.id.age_cardView);
weightCounterText = getActivity().findViewById(R.id.weight_counter_text);
ageCounterText = getActivity().findViewById(R.id.age_counter_text);
weightBtnInc = getActivity().findViewById(R.id.weight_btn_inc);
weightBtnDec = getActivity().findViewById(R.id.weight_btn_dec);
ageBtnInc = getActivity().findViewById(R.id.age_btn_inc);
ageBtnDec = getActivity().findViewById(R.id.age_btn_dec);
feetPicker = getActivity().findViewById(R.id.feet_picker);
inchPicker = getActivity().findViewById(R.id.inch_picker);
height_title_text = getActivity().findViewById(R.id.height_title_text);
calculateBtn = getActivity().findViewById(R.id.calculate_btn);
counterInit();
decimalFormat = new DecimalFormat(".#");
weightCardView.setOnClickListener(this);
ageCardView.setOnClickListener(this);
weightBtnInc.setOnClickListener(this);
weightBtnDec.setOnClickListener(this);
ageBtnInc.setOnClickListener(this);
ageBtnDec.setOnClickListener(this);
feetPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
feetValue = newVal;
heightValueIs();
}
});
inchPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
inchValue = newVal;
heightValueIs();
}
});
calculateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateBmi();
}
});
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.weight_cardView:
break;
case R.id.weight_btn_inc:
if(weightCounter < 700)
weightCounter++;
countWeight = Integer.toString(weightCounter);
weightCounterText.setText(countWeight);
break;
case R.id.weight_btn_dec:
if(weightCounter > 0)
weightCounter--;
countWeight = Integer.toString(weightCounter);
weightCounterText.setText(countWeight);
break;
case R.id.age_cardView:
break;
case R.id.age_btn_inc:
if(ageCounter < 150)
ageCounter++;
countAge = Integer.toString(ageCounter);
ageCounterText.setText(countAge);
break;
case R.id.age_btn_dec:
if(ageCounter > 1)
ageCounter--;
countAge = Integer.toString(ageCounter);
ageCounterText.setText(countAge);
break;
}
}
private void counterInit() {
countWeight = Integer.toString(weightCounter);
weightCounterText.setText(countWeight);
countAge = Integer.toString(ageCounter);
ageCounterText.setText(countAge);
feetPicker.setMinValue(1);
feetPicker.setMaxValue(8);
inchPicker.setMinValue(0);
inchPicker.setMaxValue(11);
feetPicker.setValue(5);
inchPicker.setValue(4);
heightValueIs();
}
public void heightValueIs()
{
if(inchValue == 0){
heightValue = feetValue + " feet ";
height_title_text.setText(heightValue);
}
else
heightValue = feetValue + " feet " + inchValue +" inches";
height_title_text.setText(heightValue);
}
public void calculateBmi(){
double heightInInches = feetValue * 12 + inchValue;
double heightInMetres = heightInInches / 39.37;
double heightInMetreSq = heightInMetres * heightInMetres;
double bmi = weightCounter / heightInMetreSq;
String bmiValue = decimalFormat.format(bmi);
Intent intent = new Intent(getActivity(), BmiActivity.class);
intent.putExtra("bmiVal",bmiValue);
startActivity(intent);
}
}
i have tried to check if there was some logical error but i could find none on my end Following is the log as the app crashes
2022-05-24 08:14:28.128 19168-19168/? E/e.myapplicatio: Unknown bits set in runtime_flags: 0x8000
2022-05-24 08:14:29.211 19168-19205/com.example.myapplication E/gralloc: Arm Module v1.0
2022-05-24 08:14:29.212 19168-19205/com.example.myapplication E/ion: ioctl c0044901 failed with code -1: Invalid argument
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
