我正在尝试从片段中更新片段3中的textview,但它返回nullpointerexception。但是当我从片段1更新片段2(listview)时,它完美地工作。我想知道问题是什么,有人可以指导我吗?感谢。
我的片段2代码,我试图更新我的片段3:
public void onResume() {
super.onResume();
if (getListView() != null) {
updateData();
String tagName = "android:switcher:" + R.id.pager + ":" + 2;
Bill f3 = (Bill)getActivity().getSupportFragmentManager().findFragmentByTag(tagName);
f3.updateData2();
}
}
我的片段3代码接收更新:
void updateData2() {
EventController controller = new EventController(getActivity());
Intent objIntent = getActivity().getIntent();
String eventId = objIntent.getStringExtra("eventId");
total = (TextView) getActivity().findViewById(R.id.displaytotal);
total.setText(controller.getTotal(eventId));
}
我的viewpager代码:
public class DashboardActivity2 extends FragmentActivity implements TabListener{
private ViewPager viewPager;
private TabsPagerAdapter2 mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Participant", "Expense","Bill" };
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter2(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager
.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1,
int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(arg0.getPosition());
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
这是我的LogCat:
11-16 05:46:08.912: E/AndroidRuntime(17241): FATAL EXCEPTION: main
11-16 05:46:08.912: E/AndroidRuntime(17241): java.lang.NullPointerException
11-16 05:46:08.912: E/AndroidRuntime(17241): at com.example.hapshare.Expense.onResume(Expense.java:109)
11-16 05:46:08.912: E/AndroidRuntime(17241): at android.support.v4.app.Fragment.performResume(Fragment.java:1521)
11-16 05:46:08.912: E/AndroidRuntime(17241): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:963)
11-16 05:46:08.912: E/AndroidRuntime(17241): at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:823)
11-16 05:46:08.912: E/AndroidRuntime(17241): at android.support.v4.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1128)
答案 0 :(得分:0)
我唯一能想到的是,当不可见时,片段可能会与活动分离。因此,在updateData2()中,您应该避免执行任何需要Activity的操作(如getActivity())。保存数据,在进入onResume()方法之前不要对其执行任何操作。
BTW,我发现onAttach()在onCreate()之前被调用。因此,您可以执行任何需要onCreate(),onCreateView()或onStart()以及onResume()中的Activity的内容。