Android,findViewById返回null

我正在为Android写一些代码,问题是,当我调用findViewById时,它将返回null,而我不明白为什么!从昨天开始,我一直在浪费我的大脑,但是我找不到解决办法.目标是将布局设置为listView的标题.这是我的代码,分别是标题页面

<?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>

这是我称之为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);

}
}

错误

listView.addHeaderView(header)

解决方法:

R.id.header在v内部而不在容器内部.

更改

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

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

关于Doc的ViewGroup容器说:

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.

编辑:由于您的标题视图是在另一个布局中,您也必须对其进行膨胀:

@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;
}

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);

}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...