Issue
I'm creating a converter from one currency to another and vice-versa. The method below is run with a click of a button, but it's giving an error and quitting the app.
activity_main.xml:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/LBP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:minHeight="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.367"
tools:ignore="SpeakableTextPresentCheck" />
<EditText
android:id="@+id/Dollar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:minHeight="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.542"
tools:ignore="SpeakableTextPresentCheck" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="converter"
android:text="Convert"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.672" />
<Spinner
android:id="@+id/spinner"
android:layout_width="222dp"
android:layout_height="48dp"
android:spinnerMode="dropdown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.241"
tools:ignore="SpeakableTextPresentCheck" />
<TextView
android:id="@+id/textView"
android:layout_width="50dp"
android:layout_height="27dp"
android:labelFor="@id/LBP"
android:text="LBP"
android:textAlignment="center"
android:textSize="20sp"
app:flow_horizontalAlign="center"
app:flow_verticalAlign="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.091"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.37" />
<TextView
android:id="@+id/textView2"
android:layout_width="54dp"
android:layout_height="27dp"
android:labelFor="@id/Dollar"
android:text="$"
android:textAlignment="center"
android:textSize="20sp"
app:flow_horizontalAlign="center"
app:flow_verticalAlign="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.092"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.538" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.converter;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.currencies,
android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int
i, long l) {
EditText LBP = findViewById(R.id.LBP);
EditText Dollar = findViewById(R.id.Dollar);
String text = adapterView.getItemAtPosition(i).toString();
if (text.equals("LBP")) {
LBP.setEnabled(false);
Dollar.setEnabled(true);
} else if (text.equals("DOLLAR")) {
LBP.setEnabled(true);
Dollar.setEnabled(false);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
public void converter(View view) {
int rate = 15000;
EditText LBP = (EditText) findViewById(R.id.LBP);
EditText Dollar = (EditText) findViewById(R.id.Dollar);
if (LBP.isEnabled()) {
int LBPAmount = Integer.parseInt(LBP.getText().toString());
double DollarResult = LBPAmount / rate;
Dollar.setText((int) DollarResult);
} else if (Dollar.isEnabled()) {
int DollarAmount =
Integer.parseInt(Dollar.getText().toString());
double LBPResult = DollarAmount * rate;
LBP.setText((int) LBPResult);
}
}
}
This is the error:
2021-09-11 10:51:28.996 20809-20809/com.example.converter E/ample.converte: Invalid ID
0x00003a98.
2021-09-11 10:51:28.996 20809-20809/com.example.converter D/AndroidRuntime: Shutting down VM
2021-09-11 10:51:28.998 20809-20809/com.example.converter E/AndroidRuntime: FATAL EXCEPTION:
main
Process: com.example.converter, PID: 20809
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:6597)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
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:6669)
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)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6597)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
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:6669)
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)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x3a98
at android.content.res.Resources.getText(Resources.java:348)
at android.widget.TextView.setText(TextView.java:5831)
at com.example.converter.MainActivity.converter(MainActivity.java:59)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:6597)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
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:6669)
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)
I think it's because of the Integer.parseInt, but I don't think it's written wrong. Does anyone have any idea?
Solution
The error lied within the onClick function ,i.e., converter function of your application . You tried to set value to EditText in the form of Int , Dollar.setText((int) DollarResult)
which is not the appropriate manner of doing so . You need to set String to a EditText .
So your converter function should look like this :
public void converter(View view) {
int rate = 15000;
EditText LBP = (EditText) findViewById(R.id.LBP);
EditText Dollar = (EditText) findViewById(R.id.Dollar);
if (LBP.isEnabled()) {
int LBPAmount = Integer.parseInt(LBP.getText().toString());
double DollarResult = LBPAmount / rate;
Dollar.setText(String.valueOf( (int) DollarResult));
} else if (Dollar.isEnabled()) {
int DollarAmount =
Integer.parseInt(Dollar.getText().toString());
double LBPResult = DollarAmount * rate;
LBP.setText(String.valueOf((int) LBPResult));
}
}
The change performed above is replacing Dollar.setText((int) DollarResult)
to Dollar.setText(String.valueOf( (int) DollarResult));
and same changes have been applied to LBP.
Answered By - Karunesh Palekar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.