我正在尝试更新片段3中的textview,使用supportfragmentmanager从片段2更新,我的片段1和片段2可以完美地通信,但是当从片段3更新时,我得到nullpointerexception错误。有人可以指导我吗?感谢。
我的片段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)
public class TabFragmentAdapter extends FragmentPagerAdapter {
public static final int INDEX_USER = 0;
public static final int INDEX_LOCAL_FILE = 1;
public static final int INDEX_INBOX = 2;
//
private final SparseArray<Class<? extends Fragment>> fragmentClz = new SparseArray<Class<? extends Fragment>>();
{
fragmentClz.put(INDEX_USER, UserFragment.class);
fragmentClz.put(INDEX_LOCAL_FILE, LocalFilesFragment.class);
fragmentClz.put(INDEX_INBOX, InboxFragment.class);
}
private FragmentManager fragmentManager;
public TabFragmentAdapter(FragmentManager fm) {
super(fm);
fragmentManager = fm;
}
@Override
public Fragment getItem(int arg0) {
Class<? extends Fragment> fc = fragmentClz.get(arg0);
Fragment f = null;
try {
f = fc.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return f;
}
/**
* @param index
* {@link #INDEX_INBOX #INDEX_LOCAL_FILE ...}
* @return
*/
public Fragment findFragment(int index) {
if (recorder == null) {
return null;
}
return fragmentManager.findFragmentByTag(recorder.get(index));
}
public Fragment findFragment(Class<? extends Fragment> fc) {
return findFragment(0);
}
@Override
public int getCount() {
return fragmentClz.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Object object = super.instantiateItem(container, position);
if (object instanceof Fragment) {
Fragment f = (Fragment) object;
if (recorder == null) {
recorder = new SparseArray<String>();
}
recorder.put(position, f.getTag());
}
return object;
}
private SparseArray<String> recorder;
@Override
public Parcelable saveState() {
return new FragmentIdRecorder(recorder);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
if (state instanceof FragmentIdRecorder) {
FragmentIdRecorder fir = (FragmentIdRecorder) state;
recorder = fir.getRecorder();
}
}
public static class FragmentIdRecorder implements Parcelable {
private SparseArray<String> recorder;
public SparseArray<String> getRecorder() {
return recorder;
}
public FragmentIdRecorder(SparseArray<String> recorder) {
this.recorder = recorder;
}
public FragmentIdRecorder(Parcel in) {
recorder = new SparseArray<String>();
int size = in.readInt();
if (size <= 0) {
return;
}
for (int i = 0; i < size; i++) {
recorder.put(in.readInt(), in.readString());
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
if (recorder == null || recorder.size() <= 0) {
dest.writeInt(0);
return;
} else {
int size = recorder.size();
dest.writeInt(size);
for (int i = 0; i < size; i++) {
dest.writeInt(recorder.keyAt(i));
dest.writeString(recorder.valueAt(i));
}
}
}
public static final Parcelable.Creator<FragmentIdRecorder> CREATOR = new Parcelable.Creator<FragmentIdRecorder>() {
public FragmentIdRecorder createFromParcel(Parcel in) {
return new FragmentIdRecorder(in);
}
public FragmentIdRecorder[] newArray(int size) {
return new FragmentIdRecorder[size];
}
};
}
}
使用TabFramentAdapter.findFrament(),不要使用FramentManager获取frament by tag,因为标签是由FragmentPagerAdapter创建的,我认为不是安全性(其他版本可能会更改,哈哈)