0

我没有收到任何编译错误,甚至没有出现严重警告,但是当我运行程序时,它会强制关闭,并且日志告诉我一行有某种错误。它成功了一次,但我不知道如何或何时成功。

onClick 方法:

public TextView equation;
public TextView answer11;
public TextView answer12;
public TextView or1;
public TextView or2;

public void SolveOnClick(View view){

    EditText a = (EditText)findViewById(R.id.aValue);
    EditText b = (EditText)findViewById(R.id.bValue);       //screws up somewhere here
        EditText c = (EditText)findViewById(R.id.cValue);
        equation = (TextView)findViewById(R.id.equationText);
        answer11 = (TextView)findViewById(R.id.answer1);
        answer12 = (TextView)findViewById(R.id.answer2);
        or1 = (TextView)findViewById(R.id.or1);
        or2 = (TextView)findViewById(R.id.or2);

    double A = Double.parseDouble(a.getText().toString());
        double B = Double.parseDouble(b.getText().toString());
        double C = Double.parseDouble(c.getText().toString());


        Formula(A,B,C);

    }

我的主要图形布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:onClick="SolveOnClick"
    android:text="@string/solveButtonText" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/quadFormText"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView4"
    android:layout_marginTop="17dp"
    android:layout_toLeftOf="@+id/textView4"
    android:text="@string/A"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/aValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView4"
    android:layout_alignTop="@+id/textView1"
    android:layout_marginLeft="15dp"
    android:ems="10"
    android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/bValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/aValue"
    android:layout_below="@+id/aValue"
    android:layout_marginTop="14dp"
    android:ems="10"
    android:inputType="numberDecimal" />

<EditText
    android:id="@+id/cValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/bValue"
    android:layout_below="@+id/bValue"
    android:layout_marginTop="14dp"
    android:ems="10"
    android:inputType="numberDecimal" />

<TextView
    android:id="@+id/answer1Text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:text="@string/equals1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/answer2Text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/answer1Text"
    android:layout_below="@+id/answer1Text"
    android:layout_marginTop="88dp"
    android:text="@string/equals2"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/answer1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/answer1Text"
    android:layout_toRightOf="@+id/answer1Text"
    android:text="@string/answer11"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/or1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/answer1Text"
    android:layout_below="@+id/answer1Text"
    android:layout_marginTop="25dp"
    android:text="@string/or1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/answer2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/answer2Text"
    android:layout_toRightOf="@+id/answer1Text"
    android:text="@string/answer12"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/or2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/answer2Text"
    android:layout_below="@+id/answer2Text"
    android:layout_marginTop="24dp"
    android:text="@string/or2"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/equationText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/cValue"
    android:layout_marginTop="14dp"
    android:text="@string/equation"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/cValue"
    android:layout_toLeftOf="@+id/textView4"
    android:text="@string/C"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/bValue"
    android:layout_alignLeft="@+id/textView3"
    android:text="@string/B"
    android:textAppearance="?android:attr/textAppearanceLarge" />

创建方法:

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

程序失败时的日志之一:

04-26 20:39:33.174: E/AndroidRuntime(16092): Caused by: java.lang.ClassCastException:     android.widget.TextView
04-26 20:39:33.174: E/AndroidRuntime(16092):    at calc.ulater.QuadCalcActivity.SolveOnClick(QuadCalcActivity.java:31)

我相信这与十进制文本框有关。在我让它们只取十进制数字之前,该程序运行良好,但在一两次之后它也运行良好。我宁愿不显示我的其余代码,除非你们确定问题出在那儿。

4

2 回答 2

2

如果这是搞砸的线路:

EditText b = (EditText)findViewById(R.id.bValue);

然后它说您正在尝试将 aTextView转换为EditText.

31号线是什么?那个?

于 2012-04-27T02:04:10.253 回答
2
EditText a = (EditText)findViewById(R.id.aValue);
EditText b = (EditText)findViewById(R.id.bValue);       //screws up somewhere here
EditText c = (EditText)findViewById(R.id.cValue);
equation = (TextView)findViewById(R.id.equationText);
answer11 = (TextView)findViewById(R.id.answer1);
answer12 = (TextView)findViewById(R.id.answer2);
or1 = (TextView)findViewById(R.id.or1);
or2 = (TextView)findViewById(R.id.or2);

所有初始化必须在onCreate()而不是在constructor

更新::

EditText a = (EditText)view.findViewById(R.id.aValue);
EditText b = (EditText)view.findViewById(R.id.bValue);       //screws up somewhere here
EditText c = (EditText)view.findViewById(R.id.cValue);
equation = (TextView)view.findViewById(R.id.equationText);
answer11 = (TextView)view.findViewById(R.id.answer1);
answer12 = (TextView)view.findViewById(R.id.answer2);
or1 = (TextView)view.findViewById(R.id.or1);
or2 = (TextView)view.findViewById(R.id.or2);

如果视图是您的父视图

于 2012-04-27T02:04:23.303 回答