gpt4 book ai didi

java - Bundle getInt 只返回默认值

转载 作者:行者123 更新时间:2023-12-02 05:46:59 26 4
gpt4 key购买 nike

我有一个问题,此代码仅返回默认值 -1。我尝试调试,它有值(value),所以我不知道为什么它总是返回-1。

private static final String KEY_CATEGORY_ID = "category";
Bundle bundle = getArguments();
mCategoryId = bundle.getInt(KEY_CATEGORY_ID, -1);

这是我的调试值:

bundle  Bundle  (id=830037735464)
Bundle[{category=2}]

最佳答案

在您的 Activity 中以这种方式设置参数

mFragment = new MyFragment();
Bundle extras = this.getIntent().getExtras();
extras.putInt("category", 10);
mFragment.setArguments(extras);
mFragmentTransaction = getSupportFragmentManager().beginTransaction();
mFragmentTransaction.add(R.id.profile_fragment, mFragment);
mFragmentTransaction.commit();

在fragment中这样获取

Bundle bundle = getArguments();
catgory = bundle.getInt("category");

关于java - Bundle getInt 只返回默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23965862/

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