I'm new at this
I'm new at this

Reputation: 15

Basic calculating app crash

I followed this youtube tutorial to create a basic calculating app.

I was able to debug it on my phone and it ran fine. However when I plug in the number and press calculate, the app freezes and crashes.

This is my layout:

<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" >

    <TextView
        android:id="@+id/lblTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="66dp"
        android:text="Basic Calculate"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnCalculate"
        android:layout_below="@+id/btnCalculate"
        android:layout_marginTop="23dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnCalculate"
        android:layout_alignLeft="@+id/btnCalculate"
        android:layout_marginBottom="20dp"
        android:ems="5"
        android:inputType="numberDecimal|numberSigned" />

    <Button
        android:id="@+id/btnCalculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Calculate" />

</RelativeLayout>

This is my Activity class:

package com.danielkim.danielkimcalculator;

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

public class MainActivity extends Activity {

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

        Button calc = (Button)findViewById(R.id.btnCalculate);

        calc.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                EditText number = (EditText)findViewById(R.id.num);
                TextView display = (TextView)findViewById(R.id.display);

                double num = Double.parseDouble(number.getText().toString());

                num = Double.parseDouble(samplecalc.multNum(num));

                display.setText(num + "");
            }
        });
    }

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

This is my samplecalc class for doing the calculations:

package com.danielkim.danielkimcalculator;

public class samplecalc {
    private double Num = 0;

    public samplecalc(double number) {
        Num = number;
    }

    public static String multNum(double x) {
        String re = "";

        double num = x * 5;

        re = num + "";

        return re;
    }

    public double multNum2() {
        double re = Num * 5;

        return re;
    }
}

The logcat says:

01-24 18:56:59.760: I/Adreno200-EGLSUB(12532): <ConfigWindowMatch:2081>: Format RGBA_8888.
01-24 18:57:29.640: W/dalvikvm(12532): threadid=1: thread exiting with uncaught exception (group=0x40acc1f8)
01-24 18:57:29.650: E/AndroidRuntime(12532): FATAL EXCEPTION: main
01-24 18:57:29.650: E/AndroidRuntime(12532): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
01-24 18:57:29.650: E/AndroidRuntime(12532):    at com.danielkim.danielkimcalculator.MainActivity$1.onClick(MainActivity.java:23)
01-24 18:57:29.650: E/AndroidRuntime(12532):    at android.view.View.performClick(View.java:3511)
01-24 18:57:29.650: E/AndroidRuntime(12532):    at android.view.View$PerformClick.run(View.java:14105)
01-24 18:57:29.650: E/AndroidRuntime(12532):    at android.os.Handler.handleCallback(Handler.java:605)
01-24 18:57:29.650: E/AndroidRuntime(12532):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 18:57:29.650: E/AndroidRuntime(12532):    at android.os.Looper.loop(Looper.java:137)
01-24 18:57:29.650: E/AndroidRuntime(12532):    at android.app.ActivityThread.main(ActivityThread.java:4429)
01-24 18:57:29.650: E/AndroidRuntime(12532):    at java.lang.reflect.Method.invokeNative(Native Method)
01-24 18:57:29.650: E/AndroidRuntime(12532):    at java.lang.reflect.Method.invoke(Method.java:511)
01-24 18:57:29.650: E/AndroidRuntime(12532):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
01-24 18:57:29.650: E/AndroidRuntime(12532):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
01-24 18:57:29.650: E/AndroidRuntime(12532):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 143

Answers (1)

chriskvik
chriskvik

Reputation: 1281

It looks like you are casting correct. But the compiler still complains about a faulty cast.

java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

I believe this is the EditText number = (EditText)findViewById(R.id.num); I would try to clean the project Project -> Clean if you are using Eclipse. Faults like this may happen, and the easiest solution I have found is to just clean the project and run it again.

Other threads concerning this issue :

  1. android.widget.TextView cannot be cast to android.widget.EditView
  2. java.lang.ClassCastException: android.widget.TextView cannot be cast
  3. java.lang.ClassCastException: android.widget.ImageButton cannot be cast..
  4. ClassCastException: android.widget.EditText

Upvotes: 1

Related Questions