Ich möchte Fragment-Methode in meiner MainActivity aufrufen. Das Fragment ist an ViewPager in meinem MainActivityAufruf Fragment Methode in Aktivität
ich die Methode in meinem 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();
}
}
Dies ist die Methode, die ich anrufen bin von meinem Fragment
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);
}
}
genannt haben diese meine SectionsPagerAdapter
Dies ist die Ressource-ID, mit der ich das Fragment anrufe
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/feeds_fragment"
android:background="@color/white">
Hier ist meine 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)
Ich weiß nicht, wo ich schief gelaufen.
rufen Fragment Methode Vor dem Aufruf, überprüfen Fragment-Objekt ist null oder nicht. Da Fragment in ViewPager ist, stehen nur das aktuelle Fragment und seine (rechten oder/und linken) Fragmente zur Verfügung. Andere sind zerstört. –
@HirenDabhi Ich rufe die Methode aus dem ersten Fragment im ViewPager und wie soll ich überprüfen, ob das Objekt null ist? – Savita
Wie 'if (fragment! = Null) {}' – jaibatrik