Here are my files (Conversion.xml, Conversion.java and AndroidManifest.xml). It is a small part of a project. I am able to get to the screen where I want to convert the power in watts to decibels. But when I enter the power value and click the button “Convert”, the app crashes and goes back to the my app home screen.
conversion.xml
<LinearLayout 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:orientation="vertical"
tools:context=".ConvertTodB" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter the power in Watts"
android:inputType="number"
android:text="@string/et1" />
<Button
android:id="@+id/bConvert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Convert" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Result in dB"
android:text="@string/et2"
android:textSize="20sp"/>
</LinearLayout>
Conversion.java
package com.example.rfconcepts;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.Math;
public class Conversion extends Activity {
EditText entered_val;
TextView result_val;
Button bCon;
@Override
protected void onCreate(Bundle convertTodB) {
super.onCreate(convertTodB);
setContentView(R.layout.conversion);
entered_val = (EditText) findViewById(R.string.et1);
bCon = (Button) findViewById(R.id.bConvert);
result_val = (TextView) findViewById(R.string.et2);
bCon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(entered_val.getText() != null && entered_val.getText().length() != 0){
result_val.setText(String.valueOf(10 * Math.log10(Double
.valueOf(entered_val.getText().toString()))));
}
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rfconcepts"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.rfconcepts.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.rfconcepts.Conversion"
android:label="@string/app_name">
</activity>
</application>
</manifest>
I thank you in advance for the advise.
P.S:
Here is a list of things I did after reading a lot of solutions for various questions posted here.
- Instead of using onClickListener, in conversion.xml, i added the attribute “android:onClick = “onClickConvert” and added the follwoing onClickConvert method in the Activity class.
public void onClickConvert(View v){
if(entered_val.getText() != null && entered_val.getText().length() != 0){
result_val.setText(String.valueOf(10 * Math.log10(Double
.valueOf(entered_val.getText().toString()))));
}
}
-
Changed Double.parseDouble() to Double.valueOf() (Although, I do not think this is the issue)
-
Checked if the editText itself has null, hence the if statement.
Why are you referring to a string? It should be an ID.
You did not declare an ID for your EditTexts. Do something like this:
Then you can refer to it like this:
Also do the same with your TextView.
Second issue:
EditText.getText()returns anEditable, but you need aString. Convert it like this: