java - Android inflate exception - ViewPager -
i trying implement pageviewer in app using this tutorial. app force closes. shows android.view.inflateexception: binary xml file line #2: error inflating class com.example.viewpager.scrollview
. going wrong? beginner in android please guide me. thought might have imported wrong libs app supporting api 10. searched other answers of no use. here code:
mainactvity.java
package com.example.viewpager; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentstatepageradapter; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; public class mainactivity extends fragmentactivity { /** * number of pages (wizard steps) show in demo. */ private static final int num_pages = 5; /** * pager widget, handles animation , allows swiping horizontally access previous * , next wizard steps. */ private viewpager mpager; /** * pager adapter, provides pages view pager widget. */ private pageradapter mpageradapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // instantiate viewpager , pageradapter. mpager = (viewpager) findviewbyid(r.id.pager); mpageradapter = new screenslidepageradapter(getsupportfragmentmanager()); mpager.setadapter(mpageradapter); } @override public void onbackpressed() { if (mpager.getcurrentitem() == 0) { // if user looking @ first step, allow system handle // button. calls finish() on activity , pops stack. super.onbackpressed(); } else { // otherwise, select previous step. mpager.setcurrentitem(mpager.getcurrentitem() - 1); } } /** * simple pager adapter represents 5 screenslidepagefragment objects, in * sequence. */ private class screenslidepageradapter extends fragmentstatepageradapter { public screenslidepageradapter(android.support.v4.app.fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { return new screenslidepagefragment(); } @override public int getcount() { return num_pages; } } }
screenslidepagerfragment.java
package com.example.viewpager; import android.support.v4.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class screenslidepagefragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { viewgroup rootview = (viewgroup) inflater.inflate(r.layout.fragment_screen_slide_page, container, false); return rootview; } }
actvity_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" />
fragment_slide_screen_page.xml
<?xml version="1.0" encoding="utf-8"?> <com.example.viewpager.scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent"> <textview style="?android:textappearancemedium" android:padding="16dp" android:linespacingmultiplier="1.2" android:textcolor="#000000" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="page 1" /> </com.example.viewpager.scrollview>
log cat:
android.view.inflateexception: binary xml file line #2: error inflating class com.example.viewpager.scrollview @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:581) @ android.view.layoutinflater.inflate(layoutinflater.java:386) @ android.view.layoutinflater.inflate(layoutinflater.java:320) @ com.example.viewpager.screenslidepagefragment.oncreateview(screenslidepagefragment.java:14) @ android.support.v4.app.fragment.performcreateview(fragment.java:1478) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:927) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1104) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:682) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1460) @ android.support.v4.app.fragmentmanagerimpl.executependingtransactions(fragmentmanager.java:472) @ android.support.v4.app.fragmentstatepageradapter.finishupdate(fragmentstatepageradapter.java:163) @ android.support.v4.view.viewpager.populate(viewpager.java:1068) @ android.support.v4.view.viewpager.populate(viewpager.java:914) @ android.support.v4.view.viewpager.onmeasure(viewpager.java:1436) @ android.view.view.measure(view.java:8313) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:3138) @ android.widget.framelayout.onmeasure(framelayout.java:250) @ android.view.view.measure(view.java:8313) @ android.widget.linearlayout.measurevertical(linearlayout.java:531) @ android.widget.linearlayout.onmeasure(linearlayout.java:309) @ android.view.view.measure(view.java:8313) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:3138) @ android.widget.framelayout.onmeasure(framelayout.java:250) @ android.view.view.measure(view.java:8313) @ android.view.viewroot.performtraversals(viewroot.java:845) @ android.view.viewroot.handlemessage(viewroot.java:1865) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:130) @ android.app.activitythread.main(activitythread.java:3687) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:507) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:867) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:625) @ dalvik.system.nativestart.main(native method) caused by: java.lang.classnotfoundexception: com.example.viewpager.scrollview in loader dalvik.system.pathclassloader[/data/app/com.example.viewpager-2.apk] @ dalvik.system.pathclassloader.findclass(pathclassloader.java:240) @ java.lang.classloader.loadclass(classloader.java:551) @ java.lang.classloader.loadclass(classloader.java:511) @ android.view.layoutinflater.createview(layoutinflater.java:471) @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:570) ... 33 more
change adapter to:
private class screenslidepageradapter extends fragmentstatepageradapter { int layouts[]; public screenslidepageradapter(android.support.v4.app.fragmentmanager fm) { super(fm); layouts=new int[]{r.layout.fragment_slide_screen_page1,r.layout.fragment_slide_screen_page2,r.layout.fragment_slide_screen_page3,r.layout.fragment_slide_screen_page4,r.layout.fragment_slide_screen_page5}; } @override public fragment getitem(int position) { screenslidepagefragment fragment=new screenslidepagefragment(); fragment.setcontent(layouts[position]); return fragment; } @override public int getcount() { return num_pages; } }
and in fragment:
public class screenslidepagefragment extends fragment { int layout; public void setcontent(int layout){ this.layout=layout; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { viewgroup rootview = (viewgroup) inflater.inflate(layout, container, false); return rootview; } }
although not sure change want in each fragment above require create 5 layout xmls can set every time create new fragment.
Comments
Post a Comment