我写了一个应该显示按钮的简单应用程序,单击该按钮时会显示文本:'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