Java setText使应用程序错误

时间:2011-09-28 19:39:30

标签: java android exception

我在Android中有简单的应用程序 main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:id="@+id/id_txt"
    android:text="go"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
</LinearLayout>

和TestActivity.java

package test.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TestActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView t = (TextView) findViewById(R.id.id_txt);
        t.setText("gone");
    }
}

并在android模拟器中执行一个recive消息The application Test has stopped unexpectedly. Please try again later,但为什么??

1 个答案:

答案 0 :(得分:0)

您没有正确覆盖onCreate(...)方法。它应该如下......

// NOTE: Use @Override and also the method is 'protected' and not 'public'
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView t = (TextView) findViewById(R.id.id_txt);
    t.setText("gone");
}