开发者

Java setText make application error

I have simple application in 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>

and TestActivity.java

package test.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Dialo开发者_运维知识库gInterface;
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");
    }
}

and after executing in android emulator a recive message The application Test has stopped unexpectedly. Please try again later, but why ??


You aren't overriding the onCreate(...) method correctly. It should be as follows...

// 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");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜