我寫了一個應該顯示按鈕的簡單應用程序,單擊該按鈕時會顯示文本:'Hello World' - 但是,正在做的事情正在崩潰。當我點擊按鈕後嘗試顯示HelloWorld時,Android應用程序崩潰
的XML是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
tools:context=".GraphicsActivity" />
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write Something..."
android:onClick="writeMsg" />
</RelativeLayout>
Android的源是在這裏:
public class GraphicsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graphics);
}
public void WriteMsg() {
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView tv = (TextView) findViewById(R.id.hello);
tv.setText("Hello World");
setContentView(tv);
}
});
}
}
正如我說的,它是所有崩潰。順便說一下,我在模擬器中運行它。
哪來的例外呢? – 2012-08-14 10:28:59