0

我目前正在设计一个 android 计算器应用程序,目前正在研究内存部分。如果内存不为零,我想显示“内存已保存”的措辞。

内存计算部分可以运行,但是,一旦我添加如下的措辞部分,它就会崩溃。它不能完全启动。为什么?

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:

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
4

1 回答 1

0

您正在将 TextView 转换为 EditText ,因此出现异常。

您必须已初始化Memorydisplay为 TextView。

您需要EditText Memorydisplay;在定义中将其更改为。

注意:上面的代码应该可以解决你的问题,但我不明白为什么你需要一个 EditText 来显示内存状态。一个 TextView 应该足够了。为什么不使用下面的代码?

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

无论如何,这是你的选择。祝你好运!:)

于 2012-08-25T17:09:09.030 回答