简体   繁体   中英

I am making a simple unit converter, my app crashes when i try to take value form edittext in fragments

This is my UnitConveter.java file I am making this in android studio.

when I run the app it crashes and didn't run bit when i remove/comment out the edittext part it works fine!!!

This is the StackTrace that I found but didn't know how to fix the Exception

2021-11-28 18:40:58.420 24299-24299/com.example.unitconveter E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.unitconveter, PID: 24299
java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{f658fcc VFED..CL. .F...... 82,294-542,416 #7f0901d2 app:id/value}"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at com.example.unitconveter.AreaConvert$1.onClick(AreaConvert.java:66)
    at android.view.View.performClick(View.java:6605)
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
    at android.view.View.performClickInternal(View.java:6582)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25897)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6692)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

This is the Unit Conveter File

 package com. example.Unitconverter; import android. os.Bundle; import androidx.fragment.app.Fragment; import android.text.Editable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class AreaConvert extends Fragment implements AdapterView.OnItemSelectedListener{ Spinner from; Spinner to; Button convert; EditText value; TextView answer; int Value; String val; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.fragment_area_convert,container,false); from = (Spinner) v.findViewById(R.id.from); to = (Spinner) v.findViewById(R.id.to); // value = (EditText) v.findViewById(R.id.value); // val = value.getText().toString(); // I tried to get the value through tostring but didn't help // Value = Integer.parseInt(val); // convert = (Button) v.findViewById(R.id.convert); // convert.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { //// Toast.makeText(getContext(), Value, Toast.LENGTH_SHORT).show(); // } // }); //Creating an Array to populate the spinner with the data in the string resources ArrayAdapter<CharSequence> spinnerfromAdapter = ArrayAdapter.createFromResource(getContext(),R.array.areaunits, android.R.layout.simple_spinner_item); ArrayAdapter<CharSequence> spinnertoAdapter = ArrayAdapter.createFromResource(getContext(),R.array.areaunits, android.R.layout.simple_spinner_item); spinnerfromAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnertoAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); from.setAdapter(spinnerfromAdapter); from.setOnItemSelectedListener(this); to.setAdapter(spinnertoAdapter); to.setOnItemSelectedListener(this); return v; } @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { String text = adapterView.getItemAtPosition(i).toString(); Toast.makeText(adapterView.getContext(), text, Toast.LENGTH_SHORT).show(); // switch(view.getId()){ // case R.id.from: // String program = String.valueOf(from.getSelectedItem()); // switch(program){ // case "": // default: // break; // } // break; // case R.id.to: // // break; // default: //// throw new IllegalStateException("Unexpected value: " + view.getId()); // break; // } } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }

This is my XML file

 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".AreaConvert"> <Spinner android:id="@+id/from" android:layout_width="259dp" android:layout_height="55dp" android:gravity="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.146" /> <TextView android:id="@+id/textView" android:layout_width="93dp" android:layout_height="47dp" android:text="FROM" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/from" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.168" /> <TextView android:id="@+id/textView2" android:layout_width="98dp" android:layout_height="54dp" android:layout_marginStart="4dp" android:text="TO" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/to" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.375" /> <Spinner android:id="@+id/to" android:layout_width="259dp" android:layout_height="55dp" android:gravity="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.343" /> <EditText android:id="@+id/value" android:layout_width="336dp" android:layout_height="40dp" android:ems="10" android:inputType="numberDecimal" app:layout_constraintBottom_toTopOf="@+id/to" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/from" android:autofillHints="" /> <TextView android:id="@+id/answer" android:layout_width="204dp" android:layout_height="43dp" android:text="TextView" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.516" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/to" app:layout_constraintVertical_bias="0.109" /> <Button android:id="@+id/convert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="244dp" android:text="CONVERT" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/answer" app:layout_constraintVertical_bias="0.707" /> </androidx.constraintlayout.widget.ConstraintLayout>

I want to use the value in edit text to find the answer

and also can you give me the suggestion best android development video/course(free) thanks for your help

First and foremost, you must learn some naming conventions.

Second, the StackTrace is not actual to the code you are showing. Somewhere around at AreaConvert.java line 66, must be a onClick listener which converts some value .toDouble() . The StackTrace error is telling you that that value cannot be converted to double

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM