Parikshit
Parikshit

Reputation: 1

Android Apps crashes on phone

I have just started on developing android apps and my first application crashes on my phone. I am using eclipse as SDK and my phone has andoid ver 2.3.6 Here are the details

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"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_text"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:hint="@string/edit_text"
        android:inputType="numberDecimal" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_text"
        android:layout_centerHorizontal="true"
        android:onClick="calc_fare"
        android:text="@string/calc_button" />

    <TextView
        android:id="@+id/result_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="@string/result_text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

Strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Taxi Fare Calculator</string>
    <string name="edit_text">Enter Meter Reading</string><string name="calc_button">Calculate</string><string name="result_text" >Actual Fare</string><string name="menu_settings">Settings</string>
</resources>

mainActivity.java:

package com.example.taxifarecalculator;

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

public class MainActivity extends Activity {



    @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.activity_main, menu);
        return true;

    }

    public void calc_fare() {
        EditText editText = (EditText) findViewById(R.id.edit_text);
        EditText resultText = (EditText) findViewById(R.id.result_text);
        Double mtr = Double.parseDouble(editText.getText().toString());
        Double fare=mtr*103;
        resultText.setText(fare.toString());


    }

}

The app hangs and crashes when I press the calc button which ideally should call the calc_fare method which, in turn, should populate the text view

Regards Parikshit

Upvotes: 0

Views: 364

Answers (3)

GrIsHu
GrIsHu

Reputation: 23638

Hey i have check out code and done some relevant changes. The method which you have defined in the xml file for the Button is not getting accessible ,so i have implemented the onClick of the Button explicitly and its working fine.

Check out this way:

 public class MainActivity extends Activity{
        EditText editText;
       TextView resultText;
       Button m_btn;
      View m_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    editText = (EditText) findViewById(R.id.edit_text);
 resultText = (TextView) findViewById(R.id.result_text);
    m_btn=(Button)findViewById(R.id.button1);
    m_btn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View p_v) {
            // TODO Auto-generated method stub
            calc_fare();
        }
    });
}
public void calc_fare() {       
    Double mtr = Double.parseDouble(editText.getText().toString());
    Double fare=mtr*103;
    resultText.setText(fare.toString());
}

}

Remove your onClick event from the xml file of your layout and try out this way. Its working fine absolutely.

Upvotes: 0

Usman Kurd
Usman Kurd

Reputation: 7013

You are Creating the Listener of calc_fare in your layout but your not making it to listen when you click on it and you must be caused app crashed your button is a view and your not passing view when you are handling the listener of your view Just Simpliy add View in your Method

public void Calc_fare(){

//add your code here  

}

like this

    public void Calc_fare(View view){
//add your code here 
}

now it will pick the listener and will perform the desired functionality

Upvotes: 0

Husam A. Al-ahmadi
Husam A. Al-ahmadi

Reputation: 2056

change this method arguments from

public void calc_fare() {

    }

to be like the following:

public void calc_fare(View v) {

    }

Upvotes: 2

Related Questions