Shihab Masri
Shihab Masri

Reputation: 1

When I click the Button it doesn't work (Android)

I am working in Eclipse, My app edit two integers and click the button so the app will show the average, but when i operate the app in my device, when i click the button it stop working and show message "application has been stopped"

XML File :

 <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/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:ems="10" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="Button" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="72dp"
        />

</RelativeLayout>

JAVA File :

package com.example.mine;

//import com.example.mine.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    EditText num1, num2;
    Button count;
    TextView text;
    String e1, e2;
    int q, i, r;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        count = (Button) findViewById(R.id.button1);
        num1 = (EditText) findViewById(R.id.editText1);
        num1 = (EditText) findViewById(R.id.editText2);
        text = (TextView) findViewById(R.id.text);
        count.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                e1 = num1.getText().toString();
                e2 = num2.getText().toString();

                q = Integer.parseInt(e1);
                i = Integer.parseInt(e2);

                r = (i + q) / 2;

                text.setText("the average is " + r);

                // text.setText(Integer.toString(r));
                // text.setText(r+"");
            }
        });
    }
}

Manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mine"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mine.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>

Logcat is empty

Upvotes: 0

Views: 91

Answers (1)

selbie
selbie

Reputation: 104579

Look at these two lines:

   q = Integer.parseInt(e1);
   i = Integer.parseInt(e2);

If e1 or e2 is not a number, parseInt will throw an IllegalArgument exception. In general, you should always expect parseInt has potential to throw an exception with user input - so a try/catch block is almost always warranted.

I don't seen any default text being set for edit1 or edit2, so without a logcat dump of the exception crash, that's my best guess.

Upvotes: 2

Related Questions