今天写了个小小的xml布局文件,在Activity中通过findViewById()返回布局文件中的ListView对象,然后再用该ListView对象时出现了错误(显示不了调用该布局文件的Activity),如果不用该对象就显示得了。请各位多多指教!我把代码贴上来:
Activity类:
public class Activity01 extends Activity {
ListView mListView = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mListView= (ListView)findViewById(R.id.listView);
mListView.setSelector(R.drawable.green); //如果删去这行就可以显示布局界面,如果加上就显示不了,我不明白。
setContentView(R.layout.main);
}
}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"
>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/imageView01"
android:layout_marginTop="20dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginBottom="5dip"
android:src="@drawable/bigtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:textSize="9pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/imageView01"
android:layout_marginTop="20dip"
android:text="@string/toptvstring"
/>
</RelativeLayout>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/> </LinearLayout>
Activity类:
public class Activity01 extends Activity {
ListView mListView = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mListView= (ListView)findViewById(R.id.listView);
mListView.setSelector(R.drawable.green); //如果删去这行就可以显示布局界面,如果加上就显示不了,我不明白。
setContentView(R.layout.main);
}
}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"
>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/imageView01"
android:layout_marginTop="20dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginBottom="5dip"
android:src="@drawable/bigtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:textSize="9pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/imageView01"
android:layout_marginTop="20dip"
android:text="@string/toptvstring"
/>
</RelativeLayout>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/> </LinearLayout>
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView= (ListView)findViewById(R.id.listView);
mListView.setSelector(R.drawable.green);
这样子就可以了要是像你之前那样写获取出来的mListView是null会报空指针异常,因为dalvik在你没有setContentView之前是不知道去哪里找这个list的
public void setContentView (int layoutResID)Since: API Level 1
Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.就是说将layout布局文件做解析,然后填充activity。如果不解析就去用findViewById,那是找不到的,返回的是null。
一般是放在第二行代码