问题
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
回答1:
Have you tried cleaning the project (Eclipse->Project->Clean..)? I have found that sometimes R.java gets messed up.
回答2:
Check what target have you taken, is it Android 2.2 ? It is working fine in Android2.2 . If the error continues clean & build the project and restart the eclipse.
来源:https://stackoverflow.com/questions/5635687/getviewbyid-returns-null-for-the-listview