我有以下行为。
package org.dewsworld.ui;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class DetailList extends Activity {
TextView title = (TextView) findViewById(R.id.title) ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_list);
title.setText("hello world");
}
}
哪个操作detail_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:weightSum="1">
<TextView android:id="@+id/title" android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:text="@string/headline" />
<ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent"></ListView>
</LinearLayout>
但是,当我运行它时,我收到运行时错误。 LogCat是,
答案 0 :(得分:3)
试试这个
public class DetailList extends Activity {
TextView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_list);
title = (TextView) findViewById(R.id.title) ;
title.setText("hello world");
}
}
答案 1 :(得分:2)
package org.dewsworld.ui;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class DetailList extends Activity {
TextView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_list);
title = (TextView) findViewById(R.id.title) ;;
title.setText("hello world");
}
}
它的抱怨是因为你试图使用尚未创建的Activity方法获取textView的值(因为它的Oncreate()尚未运行)