nik
nik

Reputation: 11

first android application gives error

I am new developer in Android and unfortunately my first app is giving an error. I am developing a simple temperature converter.

These are the files.

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tempconv"
    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.tempconv.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>
</application>

</manifest>

activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/ipdata"
    android:hint="Enter temperature"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="22dp"
    android:ems="10" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/ipdata"
    android:layout_below="@+id/ipdata"
    android:layout_marginTop="15dp"
    android:text="Select Conversion" />

<RadioButton
    android:id="@+id/ctof"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="25dp"
    android:layout_marginTop="18dp"
    android:text="Celsius to Feherenhit" />

<RadioButton
    android:id="@+id/ftoc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/ctof"
    android:layout_below="@+id/ctof"
    android:text="Feherenhit to Celsius" />

<EditText
    android:id="@+id/opdata"
    android:hint="Hit below button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/ftoc"
    android:layout_marginTop="54dp"
    android:ems="10" />

<Button
    android:id="@+id/doconvert"
    android:onClick="docon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/opdata"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="convert" />

</RelativeLayout>

MainActivity.java:

package com.example.tempconv;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends Activity {

    RadioButton RD1 = (RadioButton) findViewById(R.id.ctof);
    RadioButton RD2 = (RadioButton) findViewById(R.id.ftoc);
    EditText input = (EditText) findViewById(R.id.ipdata);
    EditText output = (EditText) findViewById(R.id.opdata);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void doconvert(View v) {

        float data = Float.parseFloat(input.toString());

        if (RD1.isChecked()){

            float c;
            c = (data-32)*(5/9);
            output.setText(String.valueOf(c));
        }

        else if (RD2.isChecked()){

            float f;
            f=(data*(9/5))+32;
            output.setText(String.valueOf(f));
        }   
    }
}

I checked lots of thread here but dint get solution yet.

Upvotes: 0

Views: 335

Answers (3)

Ajay S
Ajay S

Reputation: 48602

Try this.

You must call findViewById once after called the setContentView method.

private RadioButton RD1
private RadioButton RD2 
private EditText input;
private EditText output;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RD1 = (RadioButton) findViewById(R.id.ctof);
    RD2 = (RadioButton) findViewById(R.id.ftoc);
    input = (EditText) findViewById(R.id.ipdata);
    output = (EditText) findViewById(R.id.opdata);
}

Edit this line in doconvert method.

float data = Float.parseFloat(input.getText().toString());

Edit in your layout.

<EditText
    android:id="@+id/ipdata"
    android:hint="Enter temperature"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="22dp"
    android:ems="10" 
    android:inputType="number"/>   <<<Add this line to force user to enter the numeric value.

Set this input type to number because user can enter anything in edittext like text also then NumberFormatException will throw on this line.

float data = Float.parseFloat(input.getText().toString());

So you also want user should enter numeric then set the android:inputType="number" and last set the android:onClick="doconvert" in button in layout as @user113215 edited answer.

Upvotes: 0

quietmint
quietmint

Reputation: 14153

You can't findViewById() earlier than onCreate(). This is because there's no layout loaded yet, and thus no views even exist.

In other words, this code:

public class MainActivity extends Activity {
    RadioButton RD1 = (RadioButton) findViewById(R.id.ctof);
    RadioButton RD2 = (RadioButton) findViewById(R.id.ftoc);
    EditText input = (EditText) findViewById(R.id.ipdata);
    EditText output = (EditText) findViewById(R.id.opdata);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

Needs to be like this:

public class MainActivity extends Activity {
    public RadioButton RD1, RD2;
    public EditText input, output;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RD1 = (RadioButton) findViewById(R.id.ctof);
        RD2 = (RadioButton) findViewById(R.id.ftoc);
        input = (EditText) findViewById(R.id.ipdata);
        output = (EditText) findViewById(R.id.opdata);
    }

input.toString() is not the value typed in the text box. For that, you want input.getText().toString().


In your activity_main.xml file, <Button ... android:onClick="docon"> means clicking the button will fire the method docon(). You need to rename the method doconvert() or edit the XML.

Upvotes: 1

DjHacktorReborn
DjHacktorReborn

Reputation: 2928

Move this

RadioButton RD1 = (RadioButton) findViewById(R.id.ctof);
RadioButton RD2 = (RadioButton) findViewById(R.id.ftoc);
EditText input = (EditText) findViewById(R.id.ipdata);
EditText output = (EditText) findViewById(R.id.opdata);

like this

RadioButton RD1
RadioButton RD2 
EditText input;
EditText output;   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     RD1 = (RadioButton) findViewById(R.id.ctof);
        RD2 = (RadioButton) findViewById(R.id.ftoc);
        input = (EditText) findViewById(R.id.ipdata);
        output = (EditText) findViewById(R.id.opdata);

    }

Upvotes: 0

Related Questions