2

我的应用程序使用ViewPager纵向模式和横向双片段布局。

我正在尝试启动一个AsyncTask, 即 in the Fragment, from ActivityAsyncTask通常从 中的菜单项启动,Actionbar但我需要从Activity.

菜单项是一个ImageView,我在AsyncTask运行时对其进行动画处理。我的代码在双片段横向视图中运行良好,但是NullPointerException在纵向模式下我在菜单项上得到了一个。

活动

public class Main extends SherlockFragmentActivity
{
    private static List<Integer> mIds;
    private static SparseArray<Fragment> mPageReferenceMap = new SparseArray<Fragment>();

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

        setContentView(R.layout.main);

        mViewPager = (ViewPager)findViewById(R.id.viewpager); //view pager exists, so we are using the portait layout

        if (mViewPager != null)
        {
            mIds = new ArrayList<Integer>();

            mIds.add(0);
            mIds.add(1);
            mIds.add(2);

        }
        else //in landscape
        {           
            ListFragment lf = (ListFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentList);

            if (lf == null)
                lf = new ListFragment();

            DetailFragment df = (DetailFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentDetail);

            if (df == null)
            {
                df = new DetailFragment();
                df.setArguments(getIntent().getExtras());   
            }

            getSupportFragmentManager().beginTransaction().add(R.id.fragmentList, lf).commit();
            getSupportFragmentManager().beginTransaction().add(R.id.fragmentDetail, df).commit();

        }

        final MyFragmentPagerAdapter fpa = (MyFragmentPagerAdapter)mViewPager.getAdapter();
        ListFragment lf2 = (ListFragment)fpa.getFragment(0);

        //this works if I use:
        //(ListFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentList);

        lf2.RunTask();
    }       

    private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter  {  

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

        @Override  
        public Fragment getItem(int index) {        
            if (index == 0)
            {
                ListFragment lf = ListFragment.newInstance();
                mPageReferenceMap.put(index, lf);
                return lf;
            }
            else
            {
                DetailFragment df = DetailFragment.newInstance(mIds.get(index-1));
                mPageReferenceMap.put(index, df);
                return df;
        }  

        public Fragment getFragment(int key) {
            return mPageReferenceMap.get(key);
        }

        public void destroyItem(View container, int position, Object object) {
            super.destroyItem(container, position, object);
            mPageReferenceMap.remove(position);
        }

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

分段

public class ListingFragment extends SherlockListFragment
{
    private MenuItem refreshItem;

    public static ListingFragment newInstance() {

        ListingFragment lf = new ListingFragment();

        return lf;
    }

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

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

        return inflater.inflate(R.layout.listing_layout, container, false);
    }

    private void StartAnimation() {
       final LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       final ImageView ivRefresh = (ImageView)inflater.inflate(R.layout.refresh_view, null);

       final Animation rotation = AnimationUtils.loadAnimation(getActivity(), R.anim.refresh);
       ivRefresh.startAnimation(rotation);

        //this is null
       refreshItem.setActionView(ivRefresh);
    }

    public void StopAnimation()
    {
        if (refreshItem != null && refreshItem.getActionView() != null) 
        {
            refreshItem.getActionView().clearAnimation();
            refreshItem.setActionView(null);
        }
    }

    public void RunTask()
    {
        new GetItems().execute();
    }

    private class GetItems extends AsyncTask<Void, Void, Void> 
    {
        @Override
        protected void onPreExecute() 
        {
          StartAnimation();
        }

        @Override
        protected Void doInBackground(Void... unused)
        {
            ...
        }

        protected void onPostExecute(final Void unused)
        {
            StopAnimation();
        }
    }

    @Override
    public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
        inflater.inflate(R.menu.keyword_menu, menu);

        refreshItem = menu.findItem(R.id.get);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) 
    {
        if (item.getItemId() == R.id.get) {
            gi = new GetItems(getActivity(), null);
            gi.execute();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }
}   
4

1 回答 1

0

我认为以下链接为您提供了有关如何避免空指针异常的答案。Fragment 的实例可能随时被系统重新创建,这就是为什么“持有对它们的引用”并不容易。您必须使用片段管理器。

适配器实例与列表视图一起进入片段

于 2013-02-19T16:11:29.367 回答