0

This is my first time trying to implement a ViewPager. My app basically has a listing and details page and I'd like the user to be able to swipe between the details. The details are based on an id that I pass in from the listing page. The problem is that I'm calling getActivity() in the details fragment, which is coming back null. Like I said, I'm new to implementing a ViewPager so this might be something obvious:

ListingFragment.class:

public class ListingFragment extends SherlockFragmentActivity
{
    private ViewPager mViewPager;  
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter; 
    private static List<Fragment> fragments;
    public int Id = 0;

    @Override
    public void onCreate(final Bundle icicle)
    {    
        super.onCreate(icicle);

        setContentView(R.layout.main);

        mViewPager = (ViewPager)findViewById(R.id.viewpager);  
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());  
        mViewPager.setAdapter(mMyFragmentPagerAdapter);

        DetailsFragment df1 = new DetailsFragment();
        df1.Id = 1;
        df1.DisplayItems();
        fragments.add(lf);

        DetailsFragment df2 = new DetailsFragment();
        df2.Id = 2;
        df2.DisplayItems();
        fragments.add(lf);
    }

    private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {  

        public MyFragmentPagerAdapter(FragmentManager fm) {  
             super(fm);  
        }  

        @Override  
        public Fragment getItem(int index) {  
            return fragments.get(index);
        }  

        @Override  
        public int getCount() {  
             return 2;  
        }  
   }
} 

DetailsFragment.class

public class DetailsFragment extends SherlockFragmentActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(final Bundle icicle)
    {    
        super.onActivityCreated(icicle);
    }

    private void DisplayItems()
    {
        PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("value", false);
        ...
    }
}

UPDATE

I've updated my code to use static newInstance method and a Bundle to pass the id to the details fragment.

ListingFragment.class:

public class ListingFragment extends SherlockFragmentActivity
{
    private ViewPager mViewPager;  
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter; 
    private static List<Fragment> fragments;
    public int Id = 0;

    @Override
    public void onCreate(final Bundle icicle)
    {    
        super.onCreate(icicle);

        setContentView(R.layout.main);

        mViewPager = (ViewPager)findViewById(R.id.viewpager);  
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());  
        mViewPager.setAdapter(mMyFragmentPagerAdapter);

        DetailsFragment df1 =  DetailsFragment.newInstance(1);
        fragments.add(df1);

        DetailsFragment df2 =  DetailsFragment.newInstance(2);
        fragments.add(df2);
    }

    private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {  

        public MyFragmentPagerAdapter(FragmentManager fm) {  
             super(fm);  
        }  

        @Override  
        public Fragment getItem(int index) {  

            return fragments.get(index);
        }  

        @Override  
        public int getCount() {  
             return 2;  
        }  
   }
} 

DetailsFragment.class

public class DetailsFragment extends SherlockFragmentActivity
{
    private int detailId;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);         
    }

    @Override
    public void onActivityCreated(final Bundle icicle)
    {    
        super.onActivityCreated(icicle);
    }

    public static DetailsFragment newInstance(int id) {

        DetailsFragment lf = new DetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        lf.setArguments(bundle);
        return lf;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.listing, container, false);
        detailId = getArguments().getInt("id");
        DisplayItems();

        return view;
    }

    private void DisplayItems()
    {
        PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("value", false);
        ...
    }
}
4

1 に答える 1

0

そのようなアクティビティからの初期化に関係する変数を設定したり、メソッドを呼び出したりするべきではありません。フラグメントをインスタンス化するには、静的メソッドを使用してId設定する必要があります。添付のアクティビティからではなく、フラグメント内で呼び出す必要があります。setArguments(...)newInstance(...)displayItems()onCreateView(...)

詳細については、フラグメントに関する公式のAndroidガイドを参照してください。あなたは間違いなく全部を読むべきです。 http://developer.android.com/guide/components/fragments.html

于 2013-01-16T19:16:41.903 に答える