public class EditTextActivity extends Activity implements OnClickListener{
Button myButton;
EditText editText;
TextView textView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText=(EditText)findViewById(R.id.edit);
myButton=(Button)findViewById(R.id.button);
myButton.setOnClickListener(this);
textView=(TextView)findViewById(R.id.text);
}
public void onClick(View v){
if(v==myButton){
textView.setText(editText.getText());
}
}
} <Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="@string/hello"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edit"
android:hint="@string/edit"
android:password="true"/>
问题是:为什么我将 XML 中button的位置设置在最上方!就会出现在类型转换异常?editText=(EditText)findViewById(R.id.edit);是在这一行而button放在下面就可以运行了!我很无语
求各位大神们啊!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货