简体   繁体   中英

Android ViewPager and SlidingMenu error

I am trying to use SlidingMenu together with ViewPager but cannot get it to work. I have followed the simple example described by jfeinstein. When I remove the SlidingMenu from the onCreate method in MainActivity, the activity starts and displays the viewpager properly, so nothing wrong with the viewpager. How do I set the layout files correct? Many similar questions, but none of them worked for me. Do I need to specify something in my Fragments inside the viewpager?

MainActivity:

public class MainActivity extends SherlockFragmentActivity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     

    ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
    pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));


    SlidingMenu menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.LEFT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    menu.setShadowWidthRes(R.dimen.shadow_width);
    menu.setShadowDrawable(R.drawable.shadow);
    menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    menu.setFadeDegree(0.35f);
    menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    menu.setMenu(R.layout.menu);
}

.....

}

activity_main.xml

<android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

menu.xml

<?xml version="1.0" encoding="utf-8"?>
 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:name="com.jeremyfeinstein.slidingmenu.example.SampleListFragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

</fragment>

This is the error i get

E/AndroidRuntime(1780): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exampleapp/com.example.exampleapp.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment

You can not inflate to a fragment a XML that contains a fragment.

Check here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM