在活动中调用片段方法
问题描述:
我想在我的MainActivity中调用片段方法。这个片段连接到ViewPager在我的MainActivity在活动中调用片段方法
我已经调用的方法在我的MainActivity
mViewPager = (ViewPager) findViewById(R.id.pager);
setupViewPager(mViewPager);
private void setupViewPager(ViewPager mViewPager) {
mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
mSectionsPagerAdapter.addFragment(new FeedsFragment(), "Around You");
mSectionsPagerAdapter.addFragment(new InboxFragment(), "Shares");
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public void onLocationChanged(Location location) {
currentLocation = location;
if (lastLocation != null
&& geoPointFromLocation(location)
.distanceInKilometersTo(geoPointFromLocation(lastLocation)) < 0.01) {
// If the location hasn't changed by more than 10 meters, ignore it.
return;
}
lastLocation = location;
if (!hasSetUpInitialLocation) {
// Zoom to the current location.
hasSetUpInitialLocation = true;
}
FeedsFragment fragment = (FeedsFragment) getSupportFragmentManager().findFragmentById(R.id.feeds_fragment);
if(fragment != null) {
fragment.doFeedsQuery();
}
}
这是我SectionsPagerAdapter
protected Context mContext;
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SectionsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
这是我从我的片段调用方法
public void doFeedsQuery() {
Location myLoc = (MainActivity.currentLocation == null) ? MainActivity.lastLocation : MainActivity.currentLocation;
// If location info is available, load the data
if (myLoc != null) {
// Refreshes the list view with new data based
// usually on updated location data.
feedsQueryAdapter.loadObjects();
}
}
这是我打电话给片段的资源ID
这里是我的logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'void io.wyntr.peepster.Fragments.FeedsFragment.doFeedsQuery()' on a null object reference
at io.wyntr.peepster.Activities.MainActivity.onLocationChanged(MainActivity.java:687)
at com.google.android.gms.location.internal.zzk$zzb.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
我不知道我在哪里出了问题。
答
您只能在初始化后调用片段的方法。
例子:
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
Fragment yourFragment = new FeedsFragment();
fragmentTransaction.add(R.id.fragment_container, yourFragment);
fragmentTransaction.commit();
yourFragment.doFeedsQuery();
有大量的文档超过这一点,并在计算器等相关问题。
答
你的问题似乎是在这里:
FeedsFragment fragment = (FeedsFragment) getSupportFragmentManager().findFragmentById(R.id.feeds_fragment);
findFragmentById()将试图找到在XML
指定id
既然您使用的是动态方法,你没有一个。你应该改用为findFragmentByTag这样的:
FeedsFragment fragment = (FeedsFragment) getSupportFragmentManager().findFragmentByTag("Around You");
答
请使用以下FeedsFragment片段的代码中获取片段对象。
int size = mSectionsPagerAdapter.getCount();
for (int i = 0; i < size; i++) {
Fragment fragment = mSectionsPagerAdapter.getItem(i);
if (fragment != null && fragment instanceof FeedsFragment) {
((FeedsFragment)fragment).doFeedsQuery();
}
}
答
针对您的问题的两种解决方案。创建您的片段的实例作为班级成员,另一个已经告知Hiren Dabhi。
private FeedsFragment feedFragment;//Use as class instance.
private void setupViewPager(ViewPager mViewPager) {
mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
mSectionsPagerAdapter.addFragment(feedFragment, "Around You");
mSectionsPagerAdapter.addFragment(new InboxFragment(), "Shares");
mViewPager.setAdapter(mSectionsPagerAdapter);
}
,并简单地检查它不是空,isVisibleToUser然后调用你的方法
if(feedFragment != null && feedFragment.isVisible())
feedFragment.doFeedsQuery();
调用片段的方法之前,请检查片段对象为空或不是。由于片段位于ViewPager中,因此只有当前片段及其(右侧或/和左侧)片段可供访问。其他人被销毁。 –
@HirenDabhi我从viewPager中的第一个片段调用该方法,应该如何检查该对象是否为空? – Savita
像'if(fragment!= null){}' – jaibatrik