gpt4 book ai didi

java - 将变量从 Activity 传递到 fragment 时为空对象引用

转载 作者:行者123 更新时间:2023-12-01 17:44:13 24 4
gpt4 key购买 nike

将变量从 Activity 传递到 fragment 时,我收到空对象引用,但我无法找出问题所在。我试图传递的变量是currentPollName。任何帮助将不胜感激。

这是 GroupActivity:

public class GroupActivity extends AppCompatActivity {

private ViewPager myViewPager;
private TabLayout myTabLayout;
private GroupTabsAccessorAdapter myTabsAccessorAdapter;
private String currentPollName;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group);

currentPollName = getIntent().getExtras().get("pollName").toString();
Toast.makeText(GroupActivity.this, currentPollName, Toast.LENGTH_SHORT).show();

Bundle bundle = new Bundle();
bundle.putString("pollName", currentPollName);
PollFragment fragInfo = new PollFragment();
fragInfo.setArguments(bundle);

Toolbar toolbar = (Toolbar) findViewById(R.id.group_page_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(currentPollName);

myViewPager = (ViewPager) findViewById(R.id.group_tabs_pager);
myTabsAccessorAdapter = new GroupTabsAccessorAdapter(getSupportFragmentManager());
myViewPager.setAdapter(myTabsAccessorAdapter);

myTabLayout = (TabLayout) findViewById(R.id.group_tabs);
myTabLayout.setupWithViewPager(myViewPager);
}

public String getGroupName() {
return currentPollName;
}
}

这是 PollFragment OnCreateView

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
pollView = inflater.inflate(R.layout.fragment_poll, container, false);

currentPollName = getArguments().getString("pollName");


mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
GroupNameRef = FirebaseDatabase.getInstance().getReference().child("Groups").child(currentPollName);

InitializeFields();

RetrieveDisplayPollOptions();

newOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RequestNewPollOption();
}
});

submitOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SavePollOption();
}
});

return pollView;

}

这是启动 GroupActivity 的 Itent

list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String currentPollName = list_view.getItemAtPosition(position).toString();

Intent pollIntent = new Intent(getContext(), GroupActivity.class);
pollIntent.putExtra("pollName", currentPollName);
startActivity(pollIntent);
}
});

最佳答案

我在代码中发现了以下内容

  1. fragInfo fragment 不会传递给 GroupTabsAccessorAdapter。所以将 fragment 添加到适配器,然后写入myViewPager.setAdapter(myTabsAccessorAdapter);
  2. 在 PollFragment 中移动 currentPollName =
    getArguments().getString("pollName");
    onViewCreated 中,如下所示。
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
currentPollName = getArguments().getString("pollName");
}`

关于java - 将变量从 Activity 传递到 fragment 时为空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60890802/

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