Question

There are a few threads on this topic already, but unfortunately I couldn't solve my problem.

I have 2 tabs. When one tab is finished I want to update another one.

I tried the following in the first FragmentTab:

@UiThread
void updateGallery() {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.layout.gallery_layout, new GalleryFragment());
    ft.commit();
}

I get the following exception:

01-04 02:27:51.805: E/AndroidRuntime(28235): java.lang.IllegalArgumentException: No view found for id 0x7f030002 for fragment GalleryFragment{41d7dcf0 #4 id=0x7f030002}

and

@UiThread
void updateGallery() {
    // returns null (ById does also return null)
    GalleryFragment frg = (GalleryFragment) getFragmentManager().findFragmentByTag("Gallery");  
    frg.init();
}

and I also tried to update the necessary data directly:

GalleryFragment gf = new GalleryFragment();
gf.setImageUrls(findDocumentListAll());
gf.getDocumentAdapter().notifyDataSetChanged(); // returns nullpointerexception

From what I read so far the problem may be that I never add a <fragment> in my main.xml(MainActivity.java) but add them in my MainActivitylike this:

MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Gallery").setIndicator("", getResources().getDrawable(R.drawable.gallery)));
MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Camera").setIndicator("", getResources().getDrawable(R.drawable.camera)));
mTabHost.setOnTabChangedListener(this);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(MainActivity.this);

with the Tab Definition in my Adapter like:

@Override
public Fragment getItem(int index) {

    switch (index) {
        case 0:
            return new GalleryFragment_();
        case 1:
            return new CameraFragment_();
// ...
}

I'm not sure if that matters, but I always use the android.support.v4.app.Fragment; and not the android.app.Fragment;

Était-ce utile?

La solution

You must add a method in your activity with this:

void updateGallery() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.layout.gallery_layout, fragment).commit();
}

And in your fragment, you can call a method from your activity this way:

((ActivityMain) getActivity()).updateGallery();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top