2013-06-29 43 views
2

我正在为Android编写一些代码,问题在于,当我调用findViewById时它返回null,我不明白为什么!我从昨天开始一直在浪费我的大脑,但我无法弄清楚解决方案。目标是将布局设置为listView的标题。这里是我的代码,分别为我的头和我的网页:Android,findViewById返回null

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

回答

4

R.id.header是给定里面v不在里面container

变化

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

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

有关的ViewGroup容器医生说:

如果非空,这是父视图,该片段的UI应该是 附加到。片段不应该添加视图本身,但可以使用 来生成视图的LayoutParams。

编辑:既然你的头的看法是在另一个版面您可以充气太:

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

} 
+0

我已经尝试过了。但是我已经重试过了,它给了我同样的错误 – litiales

+0

在你发布的布局里面你为什么要把<?xml version =“1.0”encoding =“utf-8”?>两次?那两种不同的布局? – Blackbelt

+0

是的!我已经将标题定义为分隔布局。我不应该这样做吗? – litiales