Android的,findViewById返回null [英] Android, findViewById returns null
问题描述
我在写一些code为Android,事情是这样的,当我打电话findViewById返回我空了,我不明白为什么!我一直在浪费我的大脑从昨天起,但我不能找出解决方案。的目标是建立一个布局作为一个ListView报头。这是我的code,分别为我的头,我的网页:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /头
机器人:方向=横向
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginBottom =5DP>
<的TextView
机器人:文本=@字符串/ bydistance
机器人:layout_gravity =左
机器人:layout_width =0dp
机器人:layout_weight =1
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =13dp
机器人:重力=center_horizontal/>
<的TextView
机器人:文本=@字符串/ byactivity
机器人:layout_gravity =右
机器人:layout_width =0dp
机器人:layout_weight =1
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =13dp
机器人:重力=center_horizontal/>
< / LinearLayout中>
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:后台=#FF0000
>
<的ListView
机器人:ID =@ + ID / parkList
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =match_parent
机器人:分隔=@机器人:彩色/透明
机器人:dividerHeight =5.0dp>
< / ListView控件>
< / LinearLayout中>
在这里,我的code,我叫addheaderView:
公共类NearMeFragment扩展片段实现View.OnClickListener {
私人FragmentManager fragmentManager;
@覆盖
公共无效的onCreate(包savedBundle){
super.onCreate(savedBundle);
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedBundle){
视图V = inflater.inflate(R.layout.park_list,集装箱,假);
// TODO删除
make_test(V,集装箱);
返回伏;
}
公共无效openTest(查看视图){
新LiveComment()秀(getChildFragmentManager(),对话)。
}
公共无效的onClick(视图查看){
如果(fragmentManager == NULL)
fragmentManager = getChildFragmentManager();
//如果(view.getId()== R.id.test){
。fragmentManager.beginTransaction()增加(新LiveComment(),livecomment)提交()。
}
私人无效make_test(视图V,集装箱的ViewGroup){
ListView控件的ListView =(ListView控件)v.findViewById(R.id.parkList);
公园[]列表=新园[3];
清单[0] =新公园(Parco公司一,斯帕塔,假的,假的,1,2,3);
清单[1] =新公园(Parco公司B,佩鲁贾,假的,假的,1,2,3);
清单[2] =新公园(Parco公司B,佩鲁贾,假的,假的,1,2,3);
ParkListAdapter适配器=新ParkListAdapter(v.getContext(),R.layout.small_park_container,清单);
的LinearLayout头=(的LinearLayout)container.findViewById(R.id.header);
listView.addHeaderView(头);
listView.setAdapter(适配器);
}
}
该错误被给在
listView.addHeaderView(头)
R.id.header
里面 v
里面没有容器
。
修改
的LinearLayout头=(的LinearLayout)container.findViewById(R.id.header);
与
的LinearLayout头=(的LinearLayout)v.findViewById(R.id.header);
有关的ViewGroup容器医生说:
如果非空,这是家长认为,片断的用户界面应该是 附着。该片段不应该添加视图本身,但是这可以 被用于产生视图的的LayoutParams
编辑:既然你的头的看法是在另一个布局,你必须充气太:
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedBundle){
视图V = inflater.inflate(R.layout.park_list,集装箱,假);
查看标题= inflater.inflate(R.layout.headerlayout,NULL);
// TODO删除
make_test(五,报头);
返回伏;
}
和
私人无效make_test(视图V,查看头部){
//你的code
//你要删除的LinearLayout头=(的LinearLayout)container.findViewById(R.id.header);
listView.addHeaderView(头);
listView.setAdapter(适配器);
}
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);
}
这篇关于Android的,findViewById返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!