片段未附加到活动

时间:2012-04-30 15:54:57

标签: android android-fragments

基本上我只是在膨胀一个由两个片段组成的视图,并且在第二个布局膨胀时出现错误。

这是错误 -

04-30 11:43:22.082: E/AndroidRuntime(2269): FATAL EXCEPTION: main
04-30 11:43:22.082: E/AndroidRuntime(2269): java.lang.RuntimeException: Unable to     start activity     ComponentInfo{com.mre.test/com.mre.test.HomeActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.os.Looper.loop(Looper.java:130)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.app.ActivityThread.main(ActivityThread.java:3687)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at java.lang.reflect.Method.invoke(Method.java:507)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at dalvik.system.NativeStart.main(Native Method)
04-30 11:43:22.082: E/AndroidRuntime(2269): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at com.actionbarsherlock.internal.ActionBarSherlockCompat.setContentView(ActionBarSherlockCompat.java:841)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:306)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at com.com.mre.test.HomeActivity.onCreate(HomeActivity.java:18)
 04-30 11:43:22.082: E/AndroidRuntime(2269):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-30 11:43:22.082: E/AndroidRuntime(2269):     ... 11 more
04-30 11:43:22.082: E/AndroidRuntime(2269): Caused by: java.lang.IllegalStateException: Fragment SocialMediaFragment{4058f8c8} not attached to Activity
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.support.v4.app.Fragment.getResources(Fragment.java:551)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.support.v4.app.Fragment.getString(Fragment.java:573)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at com.mre.test.ui.SocialMediaFragment.<init>(SocialMediaFragment.java:50)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at java.lang.Class.newInstanceImpl(Native Method)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at java.lang.Class.newInstance(Class.java:1409)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.support.v4.app.Fragment.instantiate(Fragment.java:384)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.support.v4.app.Fragment.instantiate(Fragment.java:359)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:262)
04-30 11:43:22.082: E/AndroidRuntime(2269):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
04-30 11:43:22.082: E/AndroidRuntime(2269):     ... 20 more

我的家庭活动第18行:

setContentView(R.layout.activity_home);

activity_home布局:

<fragment
    android:id="@+id/fragment_dashboard"
    android:name="com.mre.test.ui.DashboardFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    class="com.mre.test.ui.DashboardFragment" >
    <!-- Preview: layout=@layout/fragment_dashboard -->
</fragment>

<fragment
    android:id="@+id/fragment_social"
    android:name="com.mre.test.ui.SocialMediaFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" 
    class="com.mre.test.ui.SocialMediaFragment"
    >
    <!-- Preview: layout=@layout/fragment_social -->
</fragment>

0 个答案:

没有答案