由于某种原因,我收到了ClassCastException错误。我真的不知道为什么。
在此先感谢,我是一名新的程序员,任何帮助都会有所帮助(没有任何双关语)。
代码:
final EditText answerBox = (EditText) findViewById(R.id.answerBox);
final Button button = (Button) findViewById(R.id.button);
final TextView problem = (TextView) findViewById(R.id.problem);
final TextView status = (TextView) findViewById(R.id.status);
final TextView num = (TextView) findViewById(R.id.numerator);
final TextView denom = (TextView) findViewById(R.id.denominator);
button.setOnClickListener(new OnClickListener()
{
private String inputString;
public void onClick(View v)
{
inputString = problem.getText().toString();
int firstNumber = Integer.parseInt(inputString.substring(0,1));
int secondNumber = Integer.parseInt(inputString.substring(2,3));
int correct = firstNumber + secondNumber;
int input;
if(!answerBox.getText().toString().equals(""))
input = Integer.parseInt(answerBox.getText().toString());
else
input = -1;
if(input != -1)
{
if(input == correct)
{
status.setText("Nice! You are correct!");
denom.setText(Integer.parseInt(denom.getText().toString()) +1);
num.setText(Integer.parseInt(num.getText().toString()) +1);
}
else
{
status.setText("Sorry, but your answer was wrong.");
denom.setText(Integer.parseInt(denom.getText().toString()) +1);
}
}
int a = (int) (Math.random() * 10);
int b = (int) (Math.random() * 10);
String newProblem = Integer.toString(a) + "+" + Integer.toString(b);
problem.setText(newProblem.toString());
answerBox.setText("");
}
});
再一次,谢谢。
来自LogCat的东西:
10-30 04:04:10.490: ERROR/AndroidRuntime(551): FATAL EXCEPTION: main
10-30 04:04:10.490: ERROR/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.benhsu.Addition/com.benhsu.Addition.AdditionActivity}: java.lang.ClassCastException: android.widget.TextView
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.os.Looper.loop(Looper.java:123)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invoke(Method.java:507)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at dalvik.system.NativeStart.main(Native Method)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): Caused by: java.lang.ClassCastException: android.widget.TextView
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at com.benhsu.Addition.AdditionActivity.onCreate(AdditionActivity.java:24)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-30 04:04:10.490: ERROR/AndroidRuntime(551): ... 11 more
XML布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:id="@+id/problem" android:layout_height="wrap_content" android:text="@string/problemString"/>
<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/answerBox">
<requestFocus></requestFocus>
</EditText>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/status" android:id="@+id/status"></TextView>
<Button android:text="Button" android:layout_height="wrap_content" android:id="@+id/button" android:layout_width="wrap_content"></Button>
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_weight="0.14">
<TextView android:layout_width="wrap_content" android:layout_alignParentLeft="true" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView1" android:text="@string/score" android:id="@+id/textView2" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignBaseline="@+id/textView1"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/numerator" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView2" android:layout_marginLeft="18dp" android:text="@string/numerator"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/fraction" android:id="@+id/fraction" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/numerator" android:layout_marginLeft="14dp"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/denominator" android:id="@+id/denominator" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/fraction" android:layout_marginLeft="19dp"></TextView>
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:55)
修改强>
它对我来说很好。在Eclipse中,转到Project-&gt; Clean ...然后重试。可能是您的R文件不同步。