gpt4 book ai didi

java - 无法恢复 Android 应用程序中的 Activity 错误

转载 作者:太空狗 更新时间:2023-10-29 14:01:42 25 4
gpt4 key购买 nike

所以我有一个 FragmentActivity,它可以正常工作。

public class ChallongeEvent extends FragmentActivity {
private TextView tab_text;
private String EVENT_ID, URL;

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

private void init() {
tab_text = (TextView) findViewById(R.id.tab_text);

Intent intent = getIntent();
EVENT_ID = intent.getStringExtra("event_id");
URL = "https://api.challonge.com/v1/tournaments/" + EVENT_ID + ".json";

String titles[] = new String[] { getString(R.string.details), getString(R.string.players) };
int numTabs = titles.length;

EventAdapter adapter = new EventAdapter(getSupportFragmentManager(), titles, numTabs);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
pager.setCurrentItem(numTabs - 1);

SlidingTabLayout sliding_tabs = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
sliding_tabs.setDistributeEvenly(true);
sliding_tabs.setViewPager(pager);
}

@Override
public void onResume() {
super.onResume();
populate();
}

private void populate() {
AsyncGet fetch = new AsyncGet(new AsyncResponse() {
@Override
public void processFinish(String output) {
for (Fragment fragment : getSupportFragmentManager().getFragments())
{
if (fragment instanceof ChallongePlayers) {
((ChallongePlayers) fragment).parsePlayers(output);
} else if (fragment instanceof ChallongeMatches) {
((ChallongeMatches) fragment).parseMatches(output);
}
}
}
});

HttpUrl.Builder urlBuilder = HttpUrl.parse(URL).newBuilder();
urlBuilder.addQueryParameter("api_key", Challonge.API_KEY);
urlBuilder.addQueryParameter("include_participants", "1");
urlBuilder.addQueryParameter("include_matches", "1");
fetch.execute(urlBuilder.build().toString());
}
}

当 FragmentActivity 启动时,它从 API 获取 JSON,然后将此 API 的数据发送到分页器中的多个 Fragment。它使用以下代码执行此操作:

        AsyncGet fetch = new AsyncGet(new AsyncResponse() {
@Override
public void processFinish(String output) {
for (Fragment fragment : getSupportFragmentManager().getFragments())
{
if (fragment instanceof ChallongePlayers) {
((ChallongePlayers) fragment).parsePlayers(output);
} else if (fragment instanceof ChallongeMatches) {
((ChallongeMatches) fragment).parseMatches(output);
}
}
}
});

HttpUrl.Builder urlBuilder = HttpUrl.parse(URL).newBuilder();
urlBuilder.addQueryParameter("api_key", Challonge.API_KEY);
urlBuilder.addQueryParameter("include_participants", "1");
urlBuilder.addQueryParameter("include_matches", "1");
fetch.execute(urlBuilder.build().toString());

但是,每个 fragment 中都有一个 SwipeRefreshLayout,我想在 AsyncTask 请求之前激活 .setRefreshing(true)。因此,我将每个 fragment 的 SwipeRefreshLayout 设为公开,并尝试在 AsyncGet fetch = ... 行上方添加以下内容:

        for (Fragment fragment : getSupportFragmentManager().getFragments())
{
if (fragment instanceof ChallongePlayers) {
((ChallongePlayers) fragment).swipe_container.setRefreshing(true);
} else if (fragment instanceof ChallongeMatches) {
((ChallongeMatches) fragment).swipe_container.setRefreshing(true);
}
}

不幸的是,这段代码的第一行产生了一个错误:

java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference

为什么这行不通?

最佳答案

getFragments() 调用返回的集合尚未初始化,为空。

您可以添加一个 if 检查以确保 getFragments() 在遍历它之前不会返回一个 null 集合:

if(getSupportFragmentManager().getFragments() != null) {
for (Fragment fragment : getSupportFragmentManager().getFragments())
{
if (fragment instanceof ChallongePlayers) {
((ChallongePlayers) fragment).swipe_container.setRefreshing(true);
} else if (fragment instanceof ChallongeMatches) {
((ChallongeMatches) fragment).swipe_container.setRefreshing(true);
}
}
}

关于java - 无法恢复 Android 应用程序中的 Activity 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35143509/

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