gpt4 book ai didi

Android,findViewById 返回 null

转载 作者:搜寻专家 更新时间:2023-11-01 08:03:40 25 4
gpt4 key购买 nike

我正在为 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.headerv 内而不是在 container 内。

改变

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

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

关于文档的 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.

编辑:由于您的标题 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,findViewById 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17378431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com