getViewByID Returns null for the Listview

26,989

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

Share:
26,989
Ishu
Author by

Ishu

yeh !!! Lets Rock !!!

Updated on August 28, 2020

Comments

  • Ishu
    Ishu over 3 years

    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>
    

    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
    

    The line to get the list view returns null.

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

    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?

    Thanks