android calculator: memory function

I am currently designing an android calculator app and is currently working on the memory part. I would like to show the wording "memory saved" if the memory is non-zero.

The memory calculation part is ok runnable, however, once I add the wording part as follows, it crashes. It cannot be started up totally. Why?

EditText Memorydisplay;
ArrayList<Double> tempfigure = new ArrayList<Double>(); 
    double tempfigure1;
    double memory1 = 0;

...

Memorydisplay = (EditText) findViewById(R.id.memoryEditText);   
        mplus = (TextView) findViewById(R.id.Mplus); 
        mminus = (TextView) findViewById(R.id.Mdeduct); 
        mclear = (TextView) findViewById(R.id.MC); 
        mrecall = (TextView) findViewById(R.id.MR);

        mplus.setOnClickListener(new View.OnClickListener() {            
            @Override 
            public void onClick(View v) { 
                tempfigure.add(Double.parseDouble(Display.getText().toString())); 
                memory1=memory1+tempfigure.get(0); 
                tempfigure1=0;
                tempfigure.removeAll(tempfigure); 
                Display.setText("0"); 
                if (memory1 >0) { Memorydisplay.setText("Memory saved");}
            } 
        });

        mminus.setOnClickListener(new View.OnClickListener() {           
            @Override 
            public void onClick(View v) { 
                tempfigure.add(Double.parseDouble(Display.getText().toString())); 
                memory1=memory1-tempfigure.get(0);
                tempfigure1=0;
                tempfigure.removeAll(tempfigure); 
                Display.setText("0");
                if (memory1 >0) { Memorydisplay.setText("Memory saved");}
            } 
        });

        mclear.setOnClickListener(new View.OnClickListener() {           
            @Override 
            public void onClick(View v) { 
                memory1=0;
                tempfigure1=0;
                tempfigure.removeAll(tempfigure); 
                Display.setText("0"); 
                if (memory1 >0) { Memorydisplay.setText("Memory saved");}
            } 
        });

        mrecall.setOnClickListener(new View.OnClickListener() {          
            @Override 
            public void onClick(View v) { 
                Display.setText(""+memory1); 
                if (memory1 >0) { Memorydisplay.setText("Memory saved");}
            } 
        });

Logcat extracted:

08-23 21:37:31.364: W/dalvikvm(2207): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
08-23 21:37:31.374: E/AndroidRuntime(2207): FATAL EXCEPTION: main
08-23 21:37:31.374: E/AndroidRuntime(2207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pearmak.jm7_calculator/com.pearmak.jm7_calculator.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
08-23 21:37:31.374: E/AndroidRuntime(2207):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at android.os.Looper.loop(Looper.java:137)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at android.app.ActivityThread.main(ActivityThread.java:4340)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at java.lang.reflect.Method.invoke(Method.java:511)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at dalvik.system.NativeStart.main(Native Method)
08-23 21:37:31.374: E/AndroidRuntime(2207): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
08-23 21:37:31.374: E/AndroidRuntime(2207):     at com.pearmak.jm7_calculator.MainActivity.onCreate(MainActivity.java:88)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at android.app.Activity.performCreate(Activity.java:4465)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-23 21:37:31.374: E/AndroidRuntime(2207):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
08-23 21:37:31.374: E/AndroidRuntime(2207):     ... 11 more
08-23 21:37:39.748: I/Process(2207): Sending signal. PID: 2207 SIG: 9

Answers


You are casting a TextView into EditText and hence the exception.

You must have initialized Memorydisplay as a TextView.

You need to change it to EditText Memorydisplay; in the definition.

Note : The above code should solve your problem but I don't understand why you need an EditText to display the memory status. A TextView should be enough for that. Why not use the below code ?

TextView Memorydisplay;
Memorydisplay = (TextView) findViewById(R.id.memoryTextView); 

Anyway, it's your choice. Good luck! :)


Need Your Help

How does theming for ziya charts work?

ruby-on-rails ruby charts

I'm implementing charts using The Ziya Charts Gem. Unfortunately, the documentation isn't really helpful or I haven't had enough coffee to figure out theming. I know I can set a theme using

Draggable line with draggable points

python matplotlib pyqt

I need to have 2 draggable points in a figureCanvas. But I have a supplementary constraint: the 2 points must be linked by a line.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.