android - Don't understand why I'm getting "Error inflating class Fragment" -
i'm learning how work fragments in android , worked on google fragment example build sample fragment. keeps giving me error
08-08 02:31:31.887: e/androidruntime(2073): fatal exception: main 08-08 02:31:31.887: e/androidruntime(2073): java.lang.runtimeexception: unable start activity componentinfo{com.example.tesingfragments/com.example.tesingfragments.mainactivity}: android.view.inflateexception: binary xml file line #12: error inflating class fragment 08-08 02:31:31.887: e/androidruntime(2073): @ android.app.activitythread.performlaunchactivity(activitythread.java:2211) 08-08 02:31:31.887: e/androidruntime(2073): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2261) 08-08 02:31:31.887: e/androidruntime(2073): @ android.app.activitythread.access$600(activitythread.java:141) 08-08 02:31:31.887: e/androidruntime(2073): @ android.app.activitythread$h.handlemessage(activitythread.java:1256) 08-08 02:31:31.887: e/androidruntime(2073): @ android.os.handler.dispatchmessage(handler.java:99) 08-08 02:31:31.887: e/androidruntime(2073): @ android.os.looper.loop(looper.java:137) 08-08 02:31:31.887: e/androidruntime(2073): @ android.app.activitythread.main(activitythread.java:5103) 08-08 02:31:31.887: e/androidruntime(2073): @ java.lang.reflect.method.invokenative(native method) 08-08 02:31:31.887: e/androidruntime(2073): @ java.lang.reflect.method.invoke(method.java:525) 08-08 02:31:31.887: e/androidruntime(2073): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 08-08 02:31:31.887: e/androidruntime(2073): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 08-08 02:31:31.887: e/androidruntime(2073): @ dalvik.system.nativestart.main(native method) 08-08 02:31:31.887: e/androidruntime(2073): caused by: android.view.inflateexception: binary xml file line #12: error inflating class fragment 08-08 02:31:31.887: e/androidruntime(2073): @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:707) 08-08 02:31:31.887: e/androidruntime(2073): @ android.view.layoutinflater.rinflate(layoutinflater.java:755) 08-08 02:31:31.887: e/androidruntime(2073): @ android.view.layoutinflater.inflate(layoutinflater.java:492) 08-08 02:31:31.887: e/androidruntime(2073): @ android.view.layoutinflater.inflate(layoutinflater.java:397) 08-08 02:31:31.887: e/androidruntime(2073): @ android.view.layoutinflater.inflate(layoutinflater.java:353) 08-08 02:31:31.887: e/androidruntime(2073): @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:267) 08-08 02:31:31.887: e/androidruntime(2073): @ android.app.activity.setcontentview(activity.java:1895) 08-08 02:31:31.887: e/androidruntime(2073): @ com.example.tesingfragments.mainactivity.oncreate(mainactivity.java:14) 08-08 02:31:31.887: e/androidruntime(2073): @ android.app.activity.performcreate(activity.java:5133) 08-08 02:31:31.887: e/androidruntime(2073): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 08-08 02:31:31.887: e/androidruntime(2073): @ android.app.activitythread.performlaunchactivity(activitythread.java:2175) 08-08 02:31:31.887: e/androidruntime(2073): ... 11 more 08-08 02:31:31.887: e/androidruntime(2073): caused by: java.lang.classnotfoundexception: didn't find class "android.view.fragment" on path: dexpathlist[[zip file "/data/app/com.example.tesingfragments-1.apk"],nativelibrarydirectories=[/data/app-lib/com.example.tesingfragments-1, /system/lib]] 08-08 02:31:31.887: e/androidruntime(2073): @ dalvik.system.basedexclassloader.findclass(basedexclassloader.java:53) 08-08 02:31:31.887: e/androidruntime(2073): @ java.lang.classloader.loadclass(classloader.java:501) 08-08 02:31:31.887: e/androidruntime(2073): @ java.lang.classloader.loadclass(classloader.java:461) 08-08 02:31:31.887: e/androidruntime(2073): @ android.view.layoutinflater.createview(layoutinflater.java:559) 08-08 02:31:31.887: e/androidruntime(2073): @ android.view.layoutinflater.oncreateview(layoutinflater.java:652) 08-08 02:31:31.887: e/androidruntime(2073): @ com.android.internal.policy.impl.phonelayoutinflater.oncreateview(phonelayoutinflater.java:66) 08-08 02:31:31.887: e/androidruntime(2073): @ android.view.layoutinflater.oncreateview(layoutinflater.java:669) 08-08 02:31:31.887: e/androidruntime(2073): @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:694) 08-08 02:31:31.887: e/androidruntime(2073): ... 21 more
this did.
mainactivity
package com.example.tesingfragments; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragment; import android.support.v4.app.listfragment; import android.view.menu; public class mainactivity extends fragmentactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
mainactivity layout
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <fragment android:name="com.example.tesingfragments.fragment1" android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.tesingfragments.fragment2" android:id="@+id/fragment2" android:layout_height="wrap_content" android:layout_width="wrap_content" /> <!-- <textview android:id="@+id/texter1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment 1" />--> </relativelayout>
fragment 1
package com.example.tesingfragments; import android.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.listfragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class fragment1 extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment return inflater.inflate(r.layout.fragmenter, container, false); } }
fragment 1 layout
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment 1" /> </linearlayout>
fragment 2
package com.example.tesingfragments; import android.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.listfragment; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class fragment2 extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment return inflater.inflate(r.layout.fragment, container, false); } }
fragment 2 layout
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment 2" /> </linearlayout>
this explorer view can see named classes properly.
replace <fragment
<fragment
in xml.
check docs more info
http://developer.android.com/guide/components/fragments.html.
edit:
also extend activity
instead of fragmentactivity
.
Comments
Post a Comment