如何将水平viewpager放在垂直viewpager中

时间:2014-06-01 10:18:19

标签: android xml android-fragments android-viewpager

我已经实现了垂直viewpager和水平viewpager单独的类。现在我试图将水平viewpager放在垂直viewpager中。但我得到了nullpointer异常

这里返回水平viewpager的方法

public ViewPager getViewPager()
{
    LayoutInflater inflater = (LayoutInflater)getSystemService
          (Context.LAYOUT_INFLATER_SERVICE);

  View rootView = inflater.inflate(R.layout.swipe_item, null, true);
  android.support.v4.view.ViewPager pag = (android.support.v4.view.ViewPager)rootView.findViewById(R.id.pager);

    mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());
    pag.setAdapter(mDemoCollectionPagerAdapter);

    return pag ;
}

这里是垂直视图寻呼机我试图放置水平viewpager

VerticalItem item  = new VerticalItem();
ViewPager pag = item.getViewPager();

mViewPager = (VerticalPager) findViewById(R.id.pager);
mViewPager.addView(pag);
mViewPager.setTag(0);

这两个类都是fragmentactivity的子类

这是我的日志猫

  

06-01 10:25:59.468:E / AndroidRuntime(2128):致命异议:主要
  06-01 10:25:59.468:E / AndroidRuntime(2128):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.infogamma / com.example.infogamma.CollectionDemoActivity}:java.lang.IllegalStateException:System onCreate()之前不可用于活动的服务   06-01 10:25:59.468:E / AndroidRuntime(2128):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
  06-01 10:25:59.468:E / AndroidRuntime(2128):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
  06-01 10:25:59.468:E / AndroidRuntime(2128):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
  06-01 10:25:59.468:E / AndroidRuntime(2128):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256)
  06-01 10:25:59.468:E / AndroidRuntime(2128):在android.os.Handler.dispatchMessage(Handler.java:99)
  06-01 10:25:59.468:E / AndroidRuntime(2128):在android.os.Looper.loop(Looper.java:137)
  06-01 10:25:59.468:E / AndroidRuntime(2128):在android.app.ActivityThread.main(ActivityThread.java:5103)
  06-01 10:25:59.468:E / AndroidRuntime(2128):at java.lang.reflect.Method.invokeNative(Native Method)
  06-01 10:25:59.468:E / AndroidRuntime(2128):at java.lang.reflect.Method.invoke(Method.java:525)
  06-01 10:25:59.468:E / AndroidRuntime(2128):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
  06-01 10:25:59.468:E / AndroidRuntime(2128):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  06-01 10:25:59.468:E / AndroidRuntime(2128):at dalvik.system.NativeStart.main(Native Method)
  06-01 10:25:59.468:E / AndroidRuntime(2128):引起:java.lang.IllegalStateException:onCreate()之前活动不可用的系统服务
  06-01 10:25:59.468:E / AndroidRuntime(2128):在android.app.Activity.getSystemService(Activity.java:4492)
  06-01 10:25:59.468:E / AndroidRuntime(2128):at com.example.infogamma.VerticalItem.getViewPager(VerticalItem.java:151)
  06-01 10:25:59.468:E / AndroidRuntime(2128):at com.example.infogamma.CollectionDemoActivity.onCreate(CollectionDemoActivity.java:63)
  06-01 10:25:59.468:E / AndroidRuntime(2128):在android.app.Activity.performCreate(Activity.java:5133)
  06-01 10:25:59.468:E / AndroidRuntime(2128):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  06-01 10:25:59.468:E / AndroidRuntime(2128):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
  06-01 10:25:59.468:E / AndroidRuntime(2128):... 11更多

这是我的collectiondemoactivity

public class CollectionDemoActivity extends FragmentActivity {

    DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;

    static String[] screens= {"makescr1","makescr2","makescr3"};

    /**
     * The {@link android.support.v4.view.ViewPager} that will display the object collection.
     */
    VerticalPager mViewPager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_image);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //ViewGroup fl =  (ViewGroup)findViewById(R.layout.full_image);
        View rootView = inflater.inflate(R.layout.swipe_item, null, false);
        android.support.v4.view.ViewPager pag = (android.support.v4.view.ViewPager)rootView.findViewById(R.id.pager);

       VerticalItem item  = new VerticalItem();
        pag = item.getViewPager();

        final ActionBar actionBar = getActionBar();

        actionBar.hide();

        //ViewPager  vp.item.getViewPager();
        // Set up the ViewPager, attaching the adapter.
        mViewPager = (VerticalPager) findViewById(R.id.pager);
        //mViewPager.setHorizontalScrollBarEnabled(true);
       mViewPager.addView(pag);
       mViewPager.setTag(0);
    }

这是我的verticalitem类

public  class VerticalItem extends FragmentActivity {

    /**
    * The {@link android.support.v4.view.PagerAdapter} that will provide fragments       representing
    * each object in a collection. We use a {@link android.support.v4.app.FragmentStatePagerAdapter}
     * derivative, which will destroy and re-create fragments as needed, saving and restoring their
     * state in the process. This is important to conserve memory and is a best practice when
     * allowing navigation between objects in a potentially large collection.
     */
    DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;

    static String[] screens= {"makescr1","makescr2","makescr3"};

    /**
     * The {@link android.support.v4.view.ViewPager} that will display the object collection.
     */
    VerticalPager mViewPager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_image);
    }

    public ViewPager getViewPager()
    {
        LayoutInflater inflater = (LayoutInflater)getSystemService
              (Context.LAYOUT_INFLATER_SERVICE);

      View rootView = inflater.inflate(R.layout.swipe_item, null, true);
      android.support.v4.view.ViewPager pag = (android.support.v4.view.ViewPager)rootView.findViewById(R.id.pager);

        mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());
        pag.setAdapter(mDemoCollectionPagerAdapter);

        return pag ;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // This is called when the Home (Up) button is pressed in the action bar.
                // Create a simple intent that starts the hierarchical parent activity and
                // use NavUtils in the Support Package to ensure proper handling of Up.
                Intent upIntent = new Intent(this, MainActivity.class);
                if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                    // This activity is not part of the application's task, so create a new task
                    // with a synthesized back stack.
                  /*  TaskStackBuilder.from(this)
                            // If there are ancestor activities, they should be added here.
                            .addNextIntent(upIntent)
                            .startActivities();
                    finish();*/
                } else {
                    // This activity is part of the application's task, so simply
                    // navigate up to the hierarchical parent activity.
                    NavUtils.navigateUpTo(this, upIntent);
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
        /**
     * A {@link android.support.v4.app.FragmentStatePagerAdapter} that returns a fragment
     * representing an object in the collection.
     */
    public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {

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

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = new DemoObjectFragment();
            Bundle args = new Bundle();
            args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1); // Our object is just an integer :-P
            args.putString("scr",screens[i]);
            fragment.setArguments(args);

            return fragment;
        }

        @Override
        public int getCount() {
            // For this contrived example, we have a 100-object collection.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "OBJECT " + (position + 1);
        }
    }

    /**
     * A dummy fragment representing a section of the app, but that simply displays dummy text.
     */
    public static class DemoObjectFragment extends Fragment {

        public static final String ARG_OBJECT = "object";

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
            Bundle args = getArguments();
            ImageView img = (ImageView)rootView.findViewById(R.id.screen);
            int resId = getResources().getIdentifier(args.getString("scr"), "drawable", "com.example.infogamma");
            img.setImageResource(resId);
            //getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit(); 
            //rootView.setRotation(-90f);            

            return rootView;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你对root视图有空,你说是附加到root:

查看rootView = inflater.inflate(R.layout.swipe_item,null,true);

要么给它一个rootView,要么使其成为真假