Quick Solution of Returning null from findViewById in AndroidGUI27

  • 2021-07-26 08:47:29
  • OfStack

When using Eclipse to develop the interface of Android and trying to obtain the interface element object through findViewById, this method sometimes returns null, which is mainly caused by the following two situations.

The first case is the most common.

For example, main. xml is as follows, in which there is one ListView, and its id is lv_contactbook


<?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"
>
<EditText android:id="@+id/et_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<ListView android:id="@+id/lv_contactbook"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

If in the code corresponding to Activity, it is written like this:


@Override
public void onCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
ListViewlv = (ListView)findViewById(R.id.lv_contactbook);
setContentView(R.layout.main);
// … 
}

That is, before setContentView is called, findViewById is called to find the interface element lv_contactbook in main layout, then the resulting lv1 must be null. The correct thing to do is to move the bold 1 line in the above code after the setContentView method call.

The second case.

In this case, LayoutInflater. inflate is usually called to convert the contents specified in the layout xml into corresponding objects. For example, there is an rowview. xml layout file as follows (for example, when customizing Adapter, it is used as the layout of 1 line content in ListView):


<?xml version="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
> 
<TextView android:id="@+id/tv_contact_id"
android:layout_width="0px"
android:layout_height="0px"
android:visibility="invisible"
android:gravity="center_vertical"
/>
<TextView android:id="@+id/tv_contactname"
android:layout_width="wrap_content"
android:layout_height="36dip"
android:textSize="16dip"
android:layout_marginTop="10dip"
android:textColor="#FFFFFFFF"
/>
</LinearLayout>

Assume that there is code similar to the following in the getView method of the custom Adapter:


View rowview = (View)inflater.inflate(R.layout.rowview, parent, false);
TextView tv_contact_id =(TextView)rowview.findViewById(R.id.tv_contact_id);
TextView tv_contactname =(TextView)rowview.findViewById(R.id.tv_contactname);

Sometimes it is found that rowview is not empty, but tv_contact_id and tv_contactname are both null! Look at the code carefully, and you can't see any errors. What is the cause? The answer is caused by Eclipse. To solve this problem, you need this project clean1 times (Project menu- > Clean submenu), which is OK.

The second case is hidden, because there is nothing wrong with the code. It will waste a lot of time if you don't think of a solution at 1 o'clock.


Related articles: