我没有编译错误甚至没有重要的警告,但是当我运行程序时,它会强制关闭并且日志告诉我在一行上有某种错误。它成功了一次,但我不知道如何或何时。
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" />
onCreate方法:
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)
我认为这与十进制文本框有关。程序运行良好之前我只使用十进制数字,但它也工作了一两次。我宁愿不显示我的其余代码,除非你们确定问题就在那里。
答案 0 :(得分:2)
如果这是搞砸的那条线:
EditText b = (EditText)findViewById(R.id.bValue);
然后它说你试图将TextView
投射到EditText
。
第31行是什么?那一个?
答案 1 :(得分: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);
如果view是您的父视图