当前位置:编程学习 > wap >>

一个小程序的xml布局文件显示不出

今天写了个小小的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>

--------------------编程问答-------------------- 你把setContentView(R.layout.main);放在前面:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
  mListView= (ListView)findViewById(R.id.listView);
  mListView.setSelector(R.drawable.green); 
这样子就可以了

要是像你之前那样写获取出来的mListView是null会报空指针异常,因为dalvik在你没有setContentView之前是不知道去哪里找这个list的
--------------------编程问答-------------------- 这样的问题很多人都问过:

看sdk
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。 --------------------编程问答-------------------- super.onCreate(savedInstanceState);

setContentView(R.layout.main);

这2句一般放前面的。

可能问题是在这。 --------------------编程问答-------------------- setContentView(R.layout.main);
一般是放在第二行代码 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答--------------------
引用 1 楼 haoyizsw 的回复:
你把setContentView(R.layout.main);放在前面:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
  mListView= (ListView)findViewById(R.id.listView);
  mListView.setSelector(R.drawable.gree……

就是这样 --------------------编程问答-------------------- --------------------编程问答--------------------
引用 2 楼 dinjay 的回复:
这样的问题很多人都问过:

看sdk
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……


学习了。。。 --------------------编程问答-------------------- 一楼说的挺对!嘻嘻 --------------------编程问答-------------------- 要在setContentView(R.layout.main);后才能通过findViewById方法获取view,否则获取的值为null,这是再对view对象进行操作,就会引发空指针异常。 --------------------编程问答-------------------- --------------------编程问答-------------------- 这是基础,楼上都是正解 --------------------编程问答--------------------
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,