当前位置: 代码迷 >> Android >> 疑难杂症求解解决思路
  详细解决方案

疑难杂症求解解决思路

热度:81   发布时间:2016-05-01 21:28:38.0
疑难杂症求解
Java code
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());         }     }    }


XML code
 <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放在下面就可以运行了!我很无语


求各位大神们啊!



------解决方案--------------------
看这代码完全没问题,我刚也copy进来运行了,也是ok的,楼主是不是别的地方报错?