06-22 11:08:17.318:E / AndroIDRuntime(31328):引起:java.lang.classCastException:androID.Widget.button无法强制转换为androID.Widget.EditText
这是我的代码:
/** Called when the user clicks the Calculate! button */public voID doCalculation(VIEw vIEw) { // Do something in response to button int answerInt; String answer; EditText numberOne = (EditText) findVIEwByID(R.ID.number1); EditText numberTwo = (EditText) findVIEwByID(R.ID.number2); int numberOnee = Integer.parseInt(numberOne.getText().toString()); int numberTwoo = Integer.parseInt(numberTwo.getText().toString()); answerInt = numberOnee * numberTwoo; answer = Integer.toString(answerInt); TextVIEw homeAnswerVIEw = (TextVIEw) findVIEwByID(R.ID.homeAnswerVIEw); homeAnswerVIEw.setTextSize(40); homeAnswerVIEw.setText(answer);}
作为参考,这里是成功启动新活动的代码:
// Called when the user clicks the Calculate! buttonpublic voID doCalculation(VIEw vIEw) { // Do something in response to button int answerInt; String answer; Intent intent = new Intent(this,displayCalculationActivity.class); EditText numberOne = (EditText) findVIEwByID(R.ID.number1); EditText numberTwo = (EditText) findVIEwByID(R.ID.number2); int numberOnee = Integer.parseInt(numberOne.getText().toString()); int numberTwoo = Integer.parseInt(numberTwo.getText().toString()); answerInt = numberOnee * numberTwoo; answer = Integer.toString(answerInt); intent.putExtra(EXTRA_MESSAGE,answer); startActivity(intent);}
UPDATE,XML供参考:
<EditText androID:ID="@+ID/number2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@+ID/textVIEw3" androID:layout_centerHorizontal="true" androID:layout_margintop="16dp" androID:ems="10" androID:inputType="number" /><EditText androID:ID="@+ID/number1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@+ID/textVIEw1" androID:layout_centerHorizontal="true" androID:ems="10" androID:inputType="number" androID:singleline="true" /><button androID:ID="@+ID/calculateBtn" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignleft="@+ID/number2" androID:layout_alignRight="@+ID/number2" androID:layout_below="@+ID/number2" androID:layout_margintop="14dp" androID:onClick="doCalculation" androID:text="Calculate!" />
谢谢您的帮助,
-Michael
编辑:原始答案不起作用,但清理项目解决了问题.
总结以上是内存溢出为你收集整理的android.widget.Button无法强制转换为android.widget.EditText全部内容,希望文章能够帮你解决android.widget.Button无法强制转换为android.widget.EditText所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)