gpt4 book ai didi

java - fragment oncreate 方法中的空容器

转载 作者:行者123 更新时间:2023-12-02 03:52:18 25 4
gpt4 key购买 nike

我正在尝试在另一项 Activity 中显示我的 fragment 。起初一切都很好,但编辑代码后我的 fragment 没有显示。这是我的主要 Activity 内容XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="ir.aftabeshafa.shafadoc.MainActivity"
tools:showIn="@layout/app_bar_main">
<fragment
android:id="@+id/headlines_fragment"
android:name="ir.aftabeshafa.shafadoc.CatFrag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

和 fragment 类:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.card_front, container, false);
for (int i = 0; i < imgs.size(); i++) {
CatModel catModel = new CatModel();
catModel.txt = cat[i];
catModel.imgid = imgs.get(i);
arrayList.add(catModel);
}
recyclerView = (RecyclerView) inflater.inflate(R.layout.card_front, container, false).findViewById(R.id.recycler);
StaggeredGridLayoutManager gridLayoutManager =
new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setHasFixedSize(true);
catAdapter = new CatAdapter(arrayList);
recyclerView.setAdapter(catAdapter);
return view;
}

主要 Activity 类:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
else {
super.onBackPressed();
}
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

}

当我调试我的应用程序时, fragment 类中的容器为空。为什么会发生这种情况?

最佳答案

当您应该使用之前已经膨胀并分配给 view 的布局时,您正在从新膨胀的布局分配 recyclverView

这意味着您返回的 View 仍然有一个空的recyclerView。要修复此更改:

recyclerView = (RecyclerView) inflater.inflate(R.layout.card_front, container, false).findViewById(R.id.recycler);

致:

recyclerView = (RecyclerView) view.findViewById(R.id.recycler);

以便您使用与您要返回的 View 关联的recyclerView

关于java - fragment oncreate 方法中的空容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35777582/

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