我是Android编程的新手,因此如果这个问题听起来很愚蠢,那么请原谅我。我正在开发一款适用于Android的计算器。所以我有大约20个按钮和一个文本字段。无论何时按下按钮,我都会更新文本字段,以便用户知道他输入的内容。在Android中设置文本
但是,当我更新文本字段时,应用程序崩溃。
updateExpression是当按下任何按钮时调用的方法。
public void updateExpression (View v)
{
Log.d("string", "updateExpression Called");
EditText text = (EditText) findViewById(R.id.name);
text.setText("HELLO EVERYONE");
}
按钮的.xml文件是:
<Button
android:id="@+id/Button03"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_below="@+id/name"
android:onClick="updateExpression"
android:text="C" />
// goes on for 20 buttons with different ids and text
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:text="@string/edit_message"
android:layout_height="33dp"
android:textColor="@color/opaque_red"/>
</RelativeLayout>
最后的logcat具有字符串数据 “叫updateExpression”。然而,之后立即崩溃。该错误标签为AndroidRuntime致命异常主。在此之后还有更多的错误。相应的一个(据我是)
造成的:java.lang.classCatException:android.widget.TextView 不能转换成android.widget.EditText
请帮助。
谢谢。
TextView text =(TextView)findViewById(R.id.name);给出的错误:TextView无法解析为类型。 –
control + shift + o添加你的导入,如果你正在使用eclipse。 – wtsang02
您需要导入。按ctrl + shift + O –