-3

I am trying to make a simple android app that will add two numbers and display the output.However, I am getting the following error continuously. "calculator.com.process stopped unexpectedly" I used API 10 for rendering layout and API 17 as compiler and Target SDK.

Could post the code finally after all these trouble.

This is the code of main_activity.java

 package com.example.calculator;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends ActionBarActivity {
    EditText txtNum1;
    EditText txtNum2;
    TextView Display;
    Button bCal;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtNum1=(EditText) findViewById(R.id.Num1);
        txtNum1=(EditText) findViewById(R.id.Num2);
        Display=(TextView)findViewById(R.id.tvDisplay);
        bCal=(Button)findViewById(R.id.bAdd);
    bCal.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View Tawfiq) {
                // TODO Auto-generated method stub
                double x=Double.parseDouble(txtNum1.getText().toString());
                double y=Double.parseDouble(txtNum2.getText().toString());
                double total=x+y;
                Display.setText("Sum is"+total);
            }
        });

    }


    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    }

The xml code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bAdd"
    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="com.example.calculator.MainActivity" >

    <EditText
        android:id="@+id/Num1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:ems="10"
        android:inputType="numberDecimal" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/Num2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Num1"
        android:layout_below="@+id/Num1"
        android:layout_marginTop="33dp"
        android:ems="10"
        android:inputType="numberDecimal" />

    <Button
        android:id="@+id/bAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvDisplay"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Button" />

    <TextView
        android:id="@+id/tvDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Num2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Sum"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

This is the logcat.

01-24 16:06:39.840: D/AndroidRuntime(1130): Shutting down VM
01-24 16:06:39.870: W/dalvikvm(1130): threadid=1: thread exiting with uncaught exception (group=0xb60164f0)
01-24 16:06:39.950: E/AndroidRuntime(1130): FATAL EXCEPTION: main
01-24 16:06:39.950: E/AndroidRuntime(1130): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-24 16:06:39.950: E/AndroidRuntime(1130):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-24 16:06:39.950: E/AndroidRuntime(1130):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-24 16:06:39.950: E/AndroidRuntime(1130):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-24 16:06:39.950: E/AndroidRuntime(1130):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 16:06:39.950: E/AndroidRuntime(1130):     at android.os.Looper.loop(Looper.java:130)
01-24 16:06:39.950: E/AndroidRuntime(1130):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-24 16:06:39.950: E/AndroidRuntime(1130):     at java.lang.reflect.Method.invokeNative(Native Method)
01-24 16:06:39.950: E/AndroidRuntime(1130):     at java.lang.reflect.Method.invoke(Method.java:507)
01-24 16:06:39.950: E/AndroidRuntime(1130):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-24 16:06:39.950: E/AndroidRuntime(1130):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-24 16:06:39.950: E/AndroidRuntime(1130):     at dalvik.system.NativeStart.main(Native Method)
01-24 16:06:39.950: E/AndroidRuntime(1130): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130):     at com.example.calculator.MainActivity.onCreate(MainActivity.java:27)
01-24 16:06:39.950: E/AndroidRuntime(1130):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-24 16:06:39.950: E/AndroidRuntime(1130):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-24 16:06:39.950: E/AndroidRuntime(1130):     ... 11 more
Tawfiq Chowdhury
  • 29
  • 1
  • 2
  • 8

1 Answers1

1

You are having problem at line no-27

Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130):     at com.example.calculator.MainActivity.onCreate(MainActivity.java:27)

Change the relativeLayouts Id in the xml file. It should not be same as for the add button.

android:id="@+id/relativeLayout"

Change it as above. Two id in the same layout cannot be same. Your add button and the relative layout is having the same id,. And during the findViewById(), relative layout is being caste in the Button. That's why class cast exception is coming.

Edit:

I compiled your code:- the next mistake you have made is-

    txtNum1=(EditText) findViewById(R.id.Num2);

It should be

    txtNum2=(EditText) findViewById(R.id.Num2);

Mistake-You are not assigning the reference to the second edittext.

Sanjeet A
  • 5,171
  • 3
  • 23
  • 40
  • Made that correction, however before seeing the comment then I followed the comment in a wrong way.Anyway, now, I can enter two numbers when I hit add button, it hangs. – Tawfiq Chowdhury Jan 24 '15 at 11:20
  • I made that correction myself yesterday but still that did not work but for some reason it worked today.You helped a lot Brother, Thanks. – Tawfiq Chowdhury Jan 25 '15 at 15:53