我有一个托管2个标签片段的活动。我从数据库中获取一些数据,并根据我想在AsyncTask完成后更新我的2个选项卡片段中的文本视图。但是当我尝试更新片段内的视图时,我得到java.lang.NullPointerException at computing.gmit.ie.scorerapp.Team1Fragment.update(Team1Fragment.java:36)
。这是代码:
public class Tabs extends FragmentActivity implements ActionBar.TabListener {
private static final String SERVER_ROOT_URI_CYPHER = "xxxxxx";
private Map<String, List<String>> map = new HashMap<>();
ActionBar actionBar;
ActionBar.Tab tab1;
ActionBar.Tab tab2;
ViewPager viewPager;
private List<String> teamList;
// list of players team 1
private List<String> team1Players = new ArrayList<>();
// list of players team 2
private List<String> team2Players = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
getActionBar().setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tab1 = actionBar.newTab();
tab1.setText("Tab 1 ");
tab1.setTabListener(this);
tab2 = actionBar.newTab();
tab2.setText("Tab 2");
tab2.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
//execute async task here
TeamPlayer teamPlayer = new TeamPlayer();
teamPlayer.execute();
}
我的适配器
class MyAdapter extends FragmentPagerAdapter {
MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragement = null;
if (position == 0) {
fragement = new Team1Fragment();
} else {
fragement = new Team2Fragment();
}
return fragement;
}
@Override
public int getCount() {
return 2;
}
}
我的异步任务 - &gt;&gt; null here
private class TeamPlayer extends AsyncTask<Void, Void, Void> {
Team1Fragment frg = new Team1Fragment();
@Override
protected Void doInBackground(Void... params) {
try {
getPlayersAndTeams(SERVER_ROOT_URI_CYPHER);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
// title of the tabs
tab1.setText(teamList.get(0));
tab2.setText(teamList.get(1));
frg.update(); ------> null
}
}
我的片段
public class Team1Fragment extends Fragment {
TextView textView;
public Team1Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_team1, container, false);
textView = (TextView) view.findViewById(R.id.tvTeam1Fragment);
return view;
}
public void update(){
textView.setText("HELLOOOO");
}
}
答案 0 :(得分:1)
Team1Fragment frg = new Team1Fragment();
那是你的问题。在将其与分页器适配器一起使用时,实例化新片段的唯一时间是在getItem()方法中,从不在其他任何地方。有关操作栏的寻呼机内部碎片,请参阅Android示例。如果您这样做,请确保正确实现所有内容,然后您可以执行异步操作。
关于你的主要问题:
如果要从异步任务更新片段,请通过引用将片段附加到任务,然后更新它,如果它仍附加到活动。
关于实施: 如果您的任务位于您的活动中,您可以从片段中通知您的活动(在onAttach()或onViewReady()中使用getActivity()),然后活动可以启动任务并将片段附加到任务。
最好用接口抽象整个机制,因此任务不是真正了解片段,而是关于实现接口TeamDataSubscriber
(或类似的东西)并获取数据的订阅者。需要setSubscribers(TeamDataSubscribers... subs)
,等等。你希望知道这些东西。