Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android converter application crashes on convert

I've built a converter in android studio and the distance part(activity) is crashing every time when I press the convert button. The application says that specific activity has stopped working and the app goes back to the Main activity. It is not showing any errors in android studio and I think my problem may be in using the int pos from the spinner but I'm not sure. This is the last element to finish for me. Please help. Below is the java followed by xml.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main4);

    spinnerFrom = (Spinner)findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapterFrom = ArrayAdapter.createFromResource(this, R.array.distance_array, android.R.layout.simple_spinner_item);
    adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerFrom.setAdapter(adapterFrom);
    spinnerFrom.setOnItemSelectedListener(this);

    spinnerTo = (Spinner)findViewById(R.id.spinner2);
    ArrayAdapter<CharSequence> adapterTo = ArrayAdapter.createFromResource(this, R.array.distance_array, android.R.layout.simple_spinner_item);
    adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerTo.setAdapter(adapterTo);
    spinnerTo.setOnItemSelectedListener(this);

}   
public void convert(View view){
    double value = new Double(distance.getText().toString());
    from = spinnerFrom.getSelectedItemPosition();
    to = spinnerTo.getSelectedItemPosition();

    if(from == 0){
        if(to == 1)
            value = UnitConverter.feetToMiles(value);
        else if (to == 2)
            value = UnitConverter.feetToMeters(value);
        else if(to == 3)
            value = UnitConverter.feetToYards(value);
        else
            value = value;
    }else if(from == 1){
        if(to == 0)
            value = UnitConverter.milesToFeet(value);
        else  if (to == 2)
            value = UnitConverter.milesToMeters(value);
        else if(to == 3)
            value = UnitConverter.milesToYards(value);
        else
            value = value;
    }else if(from == 2){
        if(to == 0)
            value =UnitConverter.metersToFeet(value);
        else if(to == 1)
            value = UnitConverter.metersToMiles(value);
        else if(to == 3)
            value = UnitConverter.metersToYards(value);
        else
            value = value;
    }else if(from == 3){
        if(to == 0)
            value = UnitConverter.yardsToFeet(value);
        else if(to == 1)
            value = UnitConverter.yardsToMiles(value);
        else if(to == 2)
            value = UnitConverter.yardsToMeters(value);
        else
            value = value;
    }
    distance.setText(new Double(value).toString());

}

and the xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.jacob.myapplication.Main4Activity"
android:background="@android:color/holo_blue_bright">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Distance Converter"
    android:id="@+id/textViewTitle"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="92dp"
    android:layout_marginStart="92dp" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_below="@+id/textViewTitle"
    android:layout_centerHorizontal="true"
    android:hint="Enter Distance" />

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinner1"
    android:spinnerMode="dialog"
    android:clickable="true"
    android:layout_below="@+id/textViewFrom"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Convert To:"
    android:id="@+id/textViewTo"
    android:layout_below="@+id/spinner1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinner2"
    android:spinnerMode="dialog"
    android:layout_below="@+id/textViewTo"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Convert From"
    android:id="@+id/textViewFrom"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Convert"
    android:id="@+id/buttonConvert"
    android:layout_below="@+id/spinner2"
    android:layout_centerHorizontal="true"
    android:onClick="convert" />

like image 516
Jacob Ricci Avatar asked Oct 19 '22 02:10

Jacob Ricci


1 Answers

You have not used same variable name here

double value = new Double(distance.getText().toString());

what is distance, where is distance? i can't see it in XML layout.

it seems you didn't change the name of EditText to distance, in edittext the id name is android:id="@+id/editText"

So change the name of distance to editText

like image 191
Kashif Anwaar Avatar answered Oct 21 '22 17:10

Kashif Anwaar