简体   繁体   中英

Android, findViewById returns null

I'm writing some code for Android, the matter is that, when I call findViewById it returns me null, and I cannot understand why! I've been wasting my brain since yesterday, but I cannot figure out the solution. The goal is to set a layout as a header for a listView. Here is my code, respectively my header and my page:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/header"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp">

    <TextView
        android:text="@string/bydistance"
        android:layout_gravity="left"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textSize="13dp"
        android:gravity="center_horizontal"/>

    <TextView
        android:text="@string/byactivity"
        android:layout_gravity="right"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textSize="13dp"
        android:gravity="center_horizontal"/>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#ff0000"
    >

    <ListView
        android:id="@+id/parkList"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:divider="@android:color/transparent"
        android:dividerHeight="5.0dp">
    </ListView>

</LinearLayout>

And here my code where I call the addheaderView:

public class NearMeFragment extends Fragment implements View.OnClickListener{

private FragmentManager fragmentManager;

@Override
public void onCreate(Bundle savedBundle){
    super.onCreate(savedBundle);
}

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {
    View v = inflater.inflate(R.layout.park_list, container, false);

    //TODO remove
    make_test(v, container);

    return v;
}

public void openTest(View view){
    new LiveComment().show(getChildFragmentManager(), "dialog");
}

public void onClick(View view){
    if (fragmentManager == null)
        fragmentManager = getChildFragmentManager();
    //if (view.getId() == R.id.test){
        fragmentManager.beginTransaction().add(new LiveComment(), "livecomment").commit();
}

private void make_test(View v, ViewGroup container) {

    ListView listView = (ListView) v.findViewById(R.id.parkList);
    Park[] list = new Park[3];
    list[0] = new Park("parco a", "acquasparta", false, false, 1, 2, 3);
    list[1]=new Park("parco b", "perugia", false, false, 1, 2, 3);
    list[2]=new Park("parco b", "perugia", false, false, 1, 2, 3);
    ParkListAdapter adapter = new ParkListAdapter(v.getContext(), R.layout.small_park_container,list);
    LinearLayout header = (LinearLayout) container.findViewById(R.id.header);
    listView.addHeaderView(header);
    listView.setAdapter(adapter);

}
}

The error is given at

listView.addHeaderView(header)

R.id.header is inside v not inside container .

Change

LinearLayout header = (LinearLayout) container.findViewById(R.id.header);

with

LinearLayout header = (LinearLayout) v.findViewById(R.id.header);

about the ViewGroup container to doc says:

If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view.

Edit: Since your header view is in another Layout you have to inflate it too:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {
    View v = inflater.inflate(R.layout.park_list, container, false);
    View header = inflater.inflate(R.layout.headerlayout, null);
    //TODO remove
    make_test(v, header);

    return v;
}

and

private void make_test(View v, View header) {

     // your code  

    // you  have to remove LinearLayout header = (LinearLayout) container.findViewById(R.id.header);
    listView.addHeaderView(header);
    listView.setAdapter(adapter);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM