java.lang.ClassCastException:android.widget.TextView。为什么会出现这个? [英] java.lang.ClassCastException: android.widget.TextView. Why am I getting this?
本文介绍了java.lang.ClassCastException:android.widget.TextView。为什么会出现这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某种原因,我得到一个ClassCastException的错误。我真的不知道为什么。
在此先感谢,我是一个新的程序员,任何帮助,将真正帮助(没有双关语意)。
code:
最后的EditText answerBox =(EditText上)findViewById(R.id.answerBox);
最后Button按钮=(按钮)findViewById(R.id.button);
最后的TextView问题=(TextView中)findViewById(R.id.problem);
最后的TextView状态=(TextView中)findViewById(R.id.status);
最后的TextView NUM =(TextView中)findViewById(R.id.numerator);
最后的TextView DENOM =(TextView中)findViewById(R.id.denominator);
button.setOnClickListener(新OnClickListener()
{
私人字符串inputString;
公共无效的onClick(视图v)
{
。inputString = problem.getText()的toString();
INT firstNumber =的Integer.parseInt(inputString.substring(0,1));
INT secondNumber =的Integer.parseInt(inputString.substring(2,3));
诠释正确= firstNumber + secondNumber;
INT输入;
如果(!answerBox.getText()。的toString()。等于())
输入=的Integer.parseInt(answerBox.getText()的toString());
其他
输入= -1;
如果(输入!= -1)
{
如果(输入==正确的)
{
status.setText(!很高兴你是正确的!);
(的Integer.parseInt(denom.getText()的toString())+1)denom.setText;
(的Integer.parseInt(num.getText()的toString())+1)num.setText;
}
其他
{
status.setText(对不起,您的答案是错的。);
(的Integer.parseInt(denom.getText()的toString())+1)denom.setText;
}
}
INT一个=(int)的(的Math.random()* 10);
INT B =(int)的(的Math.random()* 10);
串newProblem = Integer.toString(一)+++ Integer.toString(二);
problem.setText(newProblem.toString());
answerBox.setText();
}
});
再次感谢。
东西从LogCat中:
4月10日至三十日:04:10.490:ERROR / AndroidRuntime(551):致命异常:主要
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.benhsu.Addition / com.benhsu.Addition.AdditionActivity}:java.lang.ClassCastException:机器人.widget.TextView
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在android.os.Handler.dispatchMessage(Handler.java:99)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在android.os.Looper.loop(Looper.java:123)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在android.app.ActivityThread.main(ActivityThread.java:3683)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在java.lang.reflect.Method.invokeNative(本机方法)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在java.lang.reflect.Method.invoke(Method.java:507)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在dalvik.system.NativeStart.main(本机方法)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):java.lang.ClassCastException:产生的原因android.widget.TextView
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在com.benhsu.Addition.AdditionActivity.onCreate(AdditionActivity.java:24)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
4月10日至三十号:04:10.490:ERROR / AndroidRuntime(551):11 ...更多
XML布局文件:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:weightSum =1>
<的TextView
机器人:layout_width =FILL_PARENT
机器人:ID =@ + ID /问题机器人:layout_height =WRAP_CONTENT机器人:文本=@字符串/ problemString/>
< EditText上安卓layout_height =WRAP_CONTENT机器人:layout_width =match_parent机器人:ID =@ + ID / answerBox>
< requestFocus的>< / requestFocus的>
< /的EditText>
< TextView的Android版本:layout_height =WRAP_CONTENT机器人:layout_width =WRAP_CONTENT机器人:文本=@字符串/状态机器人:ID =@ + ID /状态>< / TextView的>
<按钮的android:文本=按钮机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID /按钮机器人:layout_width =WRAP_CONTENT>< /按钮>
< RelativeLayout的机器人:ID =@ + ID / relativeLayout1机器人:layout_height =WRAP_CONTENT机器人:layout_width =match_parent机器人:layout_weight =0.14>
< TextView的Android的:layout_width =WRAP_CONTENT机器人:layout_alignParentLeft =真正的机器人:layout_height =WRAP_CONTENT机器人:layout_alignBottom =@ + ID / textView1机器人:文本=@字符串/分机器人:ID = @ + ID / textView2机器人:textAppearance =机器人:ATTR / textAppearanceLarge机器人:layout_alignBaseline =?@ + ID / textView1>< / TextView的>
< TextView的Android的:机器人:ATTR / textAppearanceLargelayout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID /分子机器人textAppearance =机器人:layout_alignParentTop =真正的机器人:layout_toRightOf =@ + ID / textView2机器人:layout_marginLeft =18dp机器人:文本=@字符串/分子>< / TextView的>
< TextView的Android的:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:文本=@字符串/分机器人:ID =@ + ID /分机器人:textAppearance =?机器人:ATTR / textAppearanceLarge机器人:layout_alignParentTop =真正的机器人:layout_toRightOf =@ + ID /分子机器人:layout_marginLeft =14dp>< / TextView的>
< TextView的Android的:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:文本=@字符串/分母机器人:ID =@ + ID /分母机器人:textAppearance =机器人:ATTR / textAppearanceLarge?机器人:layout_alignParentTop =真正的机器人:layout_toRightOf =@ + ID /分机器人:layout_marginLeft =19dp>< / TextView的>
< / RelativeLayout的>
< / LinearLayout中>
解决方案
修改
它的工作对我罚款。在Eclipse中,转到工程 - >清除...,然后再试一次。这可能是你的R档是不同步的。
for some reason, I am getting a ClassCastException error. I don't really know why.
Thanks in advance, I am a new programmer and any help would really help (no pun intended).
Code:
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("");
}
});
Once again, thanks.
Stuff from 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 Layout file:
<?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>
解决方案
EDIT
It worked fine for me. In Eclipse, go to Project->Clean... and try again. It could be that your R file is out of sync.
这篇关于java.lang.ClassCastException:android.widget.TextView。为什么会出现这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文