1

这是日志猫。我的程序是计算器,我不知道如何解决这个错误

E/AndroidRuntime(620): java.lang.RuntimeException: Unable to start activity ComponentInfo{calculator.tt/calculator.tt.CalculatorActivity}: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button

这是 main.xml 文件,我的布局有一个编辑文本和 5 个表格行。在最后一个表行中,我使相等的按钮比另一个按钮大。

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >



        <EditText
            android:id="@+id/answer"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:ems="10"
            android:gravity="right|center|end"
            android:lines="1" >

            <requestFocus />
        </EditText>


        <TableRow
            android:id="@+id/TableRow02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:paddingLeft="5dp"
            android:paddingTop="30dp" >

            <Button
                android:id="@+id/open"
                android:layout_width="77dp"
                android:layout_height="wrap_content"
                android:text="(" />

            <Button
                android:id="@+id/close"
                android:layout_width="77dp"
                android:layout_height="wrap_content"
                android:text=")" />


            <ImageButton
                android:id="@+id/sqrt"
                android:layout_width="77dp"
                android:layout_height="wrap_content"
                android:src="@drawable/sqrt" />

            <Button
                android:id="@+id/add"
                android:layout_width="77dp"
                android:layout_height="wrap_content"
                android:text="+" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp" >



            <ImageButton
                android:id="@+id/x2"
                android:layout_width="77dp"
                android:layout_height="match_parent"
                android:src="@drawable/x2" />


            <ImageButton
                android:id="@+id/x3"
                android:layout_width="77dp"
                android:layout_height="wrap_content"
                android:src="@drawable/x3" />


            <Button
                android:id="@+id/giaithua"
                android:layout_width="77dp"
                android:layout_height="match_parent"
                android:text="x!" />


            <Button
                android:id="@+id/sub"
                android:layout_width="77dp"
                android:layout_height="match_parent"
                android:text="-" />

        </TableRow>

        <TableRow
            android:id="@+id/TableRow03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp" >

            <Button
                android:id="@+id/key7"
                android:layout_width="77dp"
                android:layout_height="wrap_content"
                android:text="7" />

            <Button
                android:id="@+id/key8"
                android:layout_width="77dp"
                android:layout_height="wrap_content"
                android:text="8" />

            <Button
                android:id="@+id/key9"
                android:layout_width="77dp"
                android:layout_height="wrap_content"
                android:text="9" />

            <Button
                android:id="@+id/mul"
                android:layout_width="77dp"
                android:layout_height="wrap_content"
                android:text="*" />
        </TableRow>

        <TableRow
            android:id="@+id/TableRow04"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp" >

            <Button
                android:id="@+id/key4"
                android:layout_width="77dp"
                android:layout_height="wrap_content"
                android:text="4" />

            <Button
                android:id="@+id/key5"
                android:layout_width="77dp"
                android:layout_height="wrap_content"
                android:text="5" />

            <Button
                android:id="@+id/key6"
                android:layout_width="77dp"
                android:layout_height="wrap_content"
                android:text="6" />

            <Button
                android:id="@+id/div"
                android:layout_width="77dp"
                android:layout_height="wrap_content"
                android:text="/" />
        </TableRow>

        <TableRow
            android:id="@+id/TableRow05"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <Button
                        android:id="@+id/key1"
                        android:layout_width="77dp"
                        android:layout_height="wrap_content"
                        android:text="1" />

                    <Button
                        android:id="@+id/key2"
                        android:layout_width="77dp"
                        android:layout_height="wrap_content"
                        android:text="2" />

                    <Button
                        android:id="@+id/key3"
                        android:layout_width="77dp"
                        android:layout_height="wrap_content"
                        android:text="3" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <Button
                        android:id="@+id/dot"
                        android:layout_width="77dp"
                        android:layout_height="wrap_content"
                        android:text="." />

                    <Button
                        android:id="@+id/key0"
                        android:layout_width="77dp"
                        android:layout_height="wrap_content"
                        android:text="0" />


                    <ImageButton
                        android:id="@+id/minus"
                        android:layout_width="77dp"
                        android:layout_height="match_parent"
                        android:src="@drawable/minus" />

                </LinearLayout>
            </LinearLayout>

            <Button
                android:id="@+id/Equal"
                android:layout_width="77dp"
                android:layout_height="95dp"
                android:text="=" />
        </TableRow>

    </LinearLayout>

这是计算器活动:

package calculator.tt;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;


public class CalculatorActivity extends Activity {

    EditText answer1;
    String as;
    char c;
    double a, b, e;
    boolean k;
    Button addbutton;
    Button subbutton;
    Button divbutton;
    Button mulbutton;
    ImageButton x2button;
    ImageButton x3button;
    ImageButton sqrtbutton;
    Button gtbutton;
    Button opbutton;
    Button clbutton;
    Button dotbutton;
    ImageButton minusbutton;
    Button key0button;
    Button key1button;
    Button key2button;
    Button key3button;
    Button key4button;
    Button key5button;
    Button key6button;
    Button key7button;
    Button key8button;
    Button key9button;
    Button equalbutton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /*Khai báo các lớp cho các button*/
        answer1 = (EditText)findViewById(R.id.answer);
        answer1.setInputType(InputType.TYPE_NULL);

        addbutton = (Button)findViewById(R.id.add);
        addbutton.setOnClickListener(new AddbuttonHandler());

        subbutton = (Button)findViewById(R.id.sub);
        subbutton.setOnClickListener(new SubbuttonHandler());

        divbutton = (Button)findViewById(R.id.div);
        divbutton.setOnClickListener(new DivbuttonHandler());

        mulbutton = (Button)findViewById(R.id.mul);
        mulbutton.setOnClickListener(new MulbuttonHandler());

        equalbutton = (Button)findViewById(R.id.answer);
        equalbutton.setOnClickListener(new EqualbuttonHandler());

        sqrtbutton = (ImageButton)findViewById(R.id.sqrt);
        sqrtbutton.setOnClickListener(new SqrtbuttonHandler());

        x2button = (ImageButton)findViewById(R.id.x2);
        x2button.setOnClickListener(new X2buttonHandler());

        x3button = (ImageButton)findViewById(R.id.x3);
        x3button.setOnClickListener(new X3buttonHandler());

        minusbutton = (ImageButton)findViewById(R.id.minus);
        minusbutton.setOnClickListener(new MinusbuttonHandler());

        gtbutton = (Button)findViewById(R.id.giaithua);
        gtbutton.setOnClickListener(new GtbuttonHandler());

        dotbutton = (Button)findViewById(R.id.dot);
        dotbutton.setOnClickListener(new DotbuttonHandler());

        key0button = (Button)findViewById(R.id.key0);
        key0button.setOnClickListener(new key0buttonHandler());

        key1button = (Button)findViewById(R.id.key1);
        key1button.setOnClickListener(new key1buttonHandler()); 

        key2button = (Button)findViewById(R.id.key2);
        key2button.setOnClickListener(new key2buttonHandler()); 

        key3button = (Button)findViewById(R.id.key3);
        key3button.setOnClickListener(new key3buttonHandler()); 

        key4button = (Button)findViewById(R.id.key4);
        key4button.setOnClickListener(new key4buttonHandler()); 

        key5button = (Button)findViewById(R.id.key5);
        key5button.setOnClickListener(new key5buttonHandler()); 

        key6button = (Button)findViewById(R.id.key6);
        key6button.setOnClickListener(new key6buttonHandler()); 

        key7button = (Button)findViewById(R.id.key7);
        key7button.setOnClickListener(new key7buttonHandler());

        key8button = (Button)findViewById(R.id.key8);
        key8button.setOnClickListener(new key8buttonHandler()); 

        key9button = (Button)findViewById(R.id.key9);
        key9button.setOnClickListener(new key9buttonHandler());
        Log.e("Đang chạy đến đây","Xong khai bao cac lop");
    }
    /* Các lớp cho button*/
    private class X2buttonHandler implements OnClickListener{
        public void onClick (View v){
            k=true;
            a = Double.parseDouble(answer1.getText().toString());
            answer1.setText(a*a+"");

        }
    }
    private class X3buttonHandler implements OnClickListener{
        public void onClick (View v){
            k=true;
            a = Double.parseDouble(answer1.getText().toString());
            answer1.setText(a*a*a+"");
        }
    }
    private class MinusbuttonHandler implements OnClickListener{
        public void onClick (View v){
            a = Double.parseDouble(answer1.getText().toString());
            answer1.setText(-a+"");
        }
    }
    /*Tính căn bậc 2*/
    private class SqrtbuttonHandler implements OnClickListener{
        public void onClick (View v){
            k=true;
            a = Double.parseDouble(answer1.getText().toString());
            b = Math.sqrt(a);
            answer1.setText(b+"");
        }
    }
    /*Tinh giai thua*/
    private class GtbuttonHandler implements OnClickListener{
        public void onClick (View v){
            k=true;
            a = Double.parseDouble(answer1.getText().toString());
            b=1;
            for (int i=1;i<a+1;i++)
                {
                b=b*i;
                }
            answer1.setText(b+"");
        }
    }

    private class DotbuttonHandler implements OnClickListener{
        public void onClick (View v){
            int i;
            as = answer1.getText().toString();
            if (k)
                answer1.setText("");
            k=false;
            /*Chi cho duy nhat 1 dau cham xuat hien*/
            for (i=0;i<as.length();i++)             
                if(as.charAt(i)==46)
                { i=1;
                  break;
                }
            if (i!=1)
                answer1.append("."); 
        }
    }
    private class key0buttonHandler implements OnClickListener{
        public void onClick (View v){
            if (k)
                answer1.setText("");
            answer1.append("0");
            k=false;
        }
    }
    private class key1buttonHandler implements OnClickListener{
        public void onClick (View v){               
            if (k)
                answer1.setText("");    
            answer1.append("1");
            k=false;
        }
    }
    private class key2buttonHandler implements OnClickListener{
        public void onClick (View v){
            if (k)
                answer1.setText("");        
            answer1.append("2");
            k=false;
        }
    }
    private class key3buttonHandler implements OnClickListener{
        public void onClick (View v){
            if (k)
                answer1.setText("");
            answer1.append("3");
            k=false;
        }
    }
    private class key4buttonHandler implements OnClickListener{
        public void onClick (View v){
            if (k)
                answer1.setText("");
            answer1.append("4");
            k=false;
        }
    }
    private class key5buttonHandler implements OnClickListener{
        public void onClick (View v){
            if (k)
                answer1.setText("");
            answer1.append("5");
            k=false;
        }
    }
    private class key6buttonHandler implements OnClickListener{
        public void onClick (View v){
            if (k)
                answer1.setText("");
            answer1.append("6");
            k=false;
        }
    }
    private class key7buttonHandler implements OnClickListener{
        public void onClick (View v){
            if (k)
                answer1.setText("");
            answer1.append("7");
            k=false;
        }
    }
    private class key8buttonHandler implements OnClickListener{
        public void onClick (View v){
            if (k)
                answer1.setText("");
            answer1.append("8");
            k=false;
        }
    }
    private class key9buttonHandler implements OnClickListener{
        public void onClick (View v){
            if (k)
                answer1.setText("");
            answer1.append("9");
            k=false;
        }
    }

    private class AddbuttonHandler implements OnClickListener{
        public void onClick (View v){
            int i,z=0;
            String a1=null,a2=null;
            for (i=0;i<as.length();i++)
                if (as.charAt(i)==43||as.charAt(i)==42||as.charAt(i)==45||as.charAt(i)==47){
                        a1 = as.substring(0,i-1);
                        a2 = as.substring(i+1,as.length());
                        z=as.charAt(i);
                        break;
                    }
            a = Double.parseDouble(a1);
            b = Double.parseDouble(a2);
            if (z==43)
                answer1.setText(a+b + "");
            if (z==42)
                answer1.setText(a*b + "");
            if (z==45)
                answer1.setText(a-b + "");
            else answer1.setText(a/b + "");
            answer1.append("+");
    }
}
    private class MulbuttonHandler implements OnClickListener{
        public void onClick (View v){
            int i,z=0;
            String a1=null,a2=null;
            for (i=0;i<as.length();i++)
                if (as.charAt(i)==43||as.charAt(i)==42||as.charAt(i)==45||as.charAt(i)==47){
                        a1 = as.substring(0,i-1);
                        a2 = as.substring(i+1,as.length());
                        z=as.charAt(i);
                        break;
                    }
            a = Double.parseDouble(a1);
            b = Double.parseDouble(a2);
            if (z==43)
                answer1.setText(a+b + "");
            if (z==42)
                answer1.setText(a*b + "");
            if (z==45)
                answer1.setText(a-b + "");
            else answer1.setText(a/b + "");
            answer1.append("*");
        }
    }
    private class SubbuttonHandler implements OnClickListener{
        public void onClick (View v){
            int i,z=0;
            String a1=null,a2=null;
            for (i=0;i<as.length();i++)
                if (as.charAt(i)==43||as.charAt(i)==42||as.charAt(i)==45||as.charAt(i)==47){
                        a1 = as.substring(0,i-1);
                        a2 = as.substring(i+1,as.length());
                        z=as.charAt(i);
                        break;
                    }
            a = Double.parseDouble(a1);
            b = Double.parseDouble(a2);
            if (z==43)
                answer1.setText(a+b + "");
            if (z==42)
                answer1.setText(a*b + "");
            if (z==45)
                answer1.setText(a-b + "");
            else answer1.setText(a/b + "");
            answer1.append("-");
        }
    }
    private class DivbuttonHandler implements OnClickListener{
        public void onClick (View v){
            int i,z=0;
            String a1=null,a2=null;
            for (i=0;i<as.length();i++)
                if (as.charAt(i)==43||as.charAt(i)==42||as.charAt(i)==45||as.charAt(i)==47){
                        a1 = as.substring(0,i-1);
                        a2 = as.substring(i+1,as.length());
                        z=as.charAt(i);
                        break;
                    }
            a = Double.parseDouble(a1);
            b = Double.parseDouble(a2);
            if (z==43)
                answer1.setText(a+b + "");
            if (z==42)
                answer1.setText(a*b + "");
            if (z==45)
                answer1.setText(a-b + "");
            else answer1.setText(a/b + "");
            answer1.append("/");
        }
    }
    private class EqualbuttonHandler implements OnClickListener{
        public void onClick (View v){
            int i,z=0;
            String a1 = null, a2 = null;
            k=true;
            as = answer1.getText().toString();
            for (i=0;i<as.length();i++)
                if (as.charAt(i)==43||as.charAt(i)==42||as.charAt(i)==45||as.charAt(i)==47){
                        a1 = as.substring(0,i-1);
                        a2 = as.substring(i+1,as.length());
                        z=as.charAt(i);
                        break;
                    }
            a = Double.parseDouble(a1);
            b = Double.parseDouble(a2);
            if (z==43)
                answer1.setText(a+b + "");
            if (z==42)
                answer1.setText(a*b + "");
            if (z==45)
                answer1.setText(a-b + "");
            else answer1.setText(a/b + "");
            }
        }
    }
4

1 回答 1

2

在此 Java 行中:
equalbutton = (Button)findViewById(R.id.answer);

您正在查找由此 XML 定义的 EditText:
<EditText
        android:id="@+id/answer"

看起来您打算这样写:
equalbutton = (Button)findViewById(R.id.Equal);

于 2012-06-05T02:49:38.133 回答