getViewByID为Listview返回null

getViewByID为Listview返回null

本文介绍了getViewByID为Listview返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的main.xml如下:

I have got my main.xml as follows:

<?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:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <ListView android:layout_height="wrap_content" android:id="@+id/listView1" android:layout_width="match_parent"></ListView>
</LinearLayout>

现在,我想在活动的onCreate事件中获取ID为listView1的ListView.这是我为实现这些目的而编写的代码..

Now i want to get the ListView with id listView1 in the onCreate event of the activity. Here goes my code to achieve this stuff ..

public class TwitterClient extends Activity {
    private static ListView mTweetsList;

     @SuppressWarnings("deprecation")
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main); // Set the content view.
            TextView txtView = (TextView)findViewById(R.id.textView1); // This works as expected
            mTweetsList = (ListView)findViewById(R.id.listView1); // This returns null

获取列表视图的行返回null.

The line to get the list view returns null.

mTweetsList = (ListView)findViewById(R.id.listView1); -- Does not work and returns null !!

当我使用相同的功能在相同的布局中通过id查找textview时,一切正常.谁能指出我这里可能是错误的地方?

where as when i use the same function to find the textview by id in the same layout, it works all fine. Can anyone point me to what might be an error here?

谢谢

推荐答案

您是否尝试过清理项目(Eclipse-> Project-> Clean ..)?我发现有时R.java会搞砸.

Have you tried cleaning the project (Eclipse->Project->Clean..)? I have found that sometimes R.java gets messed up.

这篇关于getViewByID为Listview返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:42