gpt4 book ai didi

Android fragment - findViewById 返回 null

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:49 28 4
gpt4 key购买 nike

<分区>

我有一个在互联网上似乎很常见的问题:我创建了一个只有 fragment 的 Activity 。这是生成的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
ListView lv = (ListView) findViewById(android.R.id.list);
}
}

条件之后或内部,我无法使用 findViewById 获取任何对象:它始终为 null。这是生成的代码的其余部分:

public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_customer,
container, false);
return rootView;
}

activity_customer.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.axacs.marc.CustomerActivity"
tools:ignore="MergeRootFrame" />

ListView 实际上在 fragment_customer.xml 中:

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/new_folder_button"
android:text="" />

有谁知道缺少什么吗?

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