一个简单的安卓Hello World:
MainActivity.java:
package com.amaker.ch02.app;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private TextView displayTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
displayTextView = (TextView)findViewById(R.id.DisplayTextView);
displayTextView.setText("change in the code"); <--Right if delete the line
}
}
资源/布局/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id = "@+id/DisplayTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
运行,android会显示:抱歉,应用程序意外停止。请再试一次。,但正如我在代码中指出的那样MainActivity.java
,如果我删除代码displayTextView.setText("change in the code");
,一切都可以。
代码行有什么问题?