我想在我的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)
我不知道我在哪里出了问题。
调用片段的方法之前,请检查片段对象为空或不是。由于片段位于ViewPager中,因此只有当前片段及其(右侧或/和左侧)片段可供访问。其他人被销毁。 –
@HirenDabhi我从viewPager中的第一个片段调用该方法,应该如何检查该对象是否为空? – Savita
像'if(fragment!= null){}' – jaibatrik