所以我有一个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的数据发送到寻呼机内的多个片段。它使用以下代码执行此操作:
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());
但是,每个片段中都有一个SwipeRefreshLayout
,我希望在AsyncTask请求之前激活.setRefreshing(true)
。所以我将每个片段上的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:尝试调用接口方法' java.util.Iterator java.util.List.iterator()'在空对象引用上
为什么这不起作用?
答案 0 :(得分:1)
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);
}
}
}