具有兼容性库的片段

时间:2012-02-14 15:43:37

标签: android android-fragments

我试图以最简单的方式使用片段。我希望有一个显示一组按钮的第一个按钮,这些按钮将导致不同的按钮。 我使用兼容性库,似乎包含得很好,我的所有导入都是正确的。

这是我试图这样做的方式:

主要:

public class MainActivity extends FragmentActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

main.xml:

<?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" >

    <fragment
        android:id="@+id/titles_frag"
        android:name="com.clicmobile.cannes.TitlesFragments"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </fragment>

    <FrameLayout
        android:id="@+id/details"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

我的TitlesFragments:

public class TitlesFragments extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View mainView = inflater.inflate(R.layout.title_fragment, container,
                false);


        GrigFragment details = (GrigFragment) getFragmentManager()
                .findFragmentById(R.id.details);

解决方案: GridFragment details = new GridFragment();

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.details, details);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();

        return mainView;

    }
}

title_fragment.xml:

<?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" >
    <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:id="@+id/myButton"
     android:text="lol"
     />

</LinearLayout>

GridFragment:

public class GrigFragment extends Fragment{

     public static GrigFragment newInstance() {
         GrigFragment f = new GrigFragment();



            return f;
        }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View mainView = inflater.inflate(R.layout.title_fragment_2, container, false);      
        return mainView;


    }
}

我的title_fragment_2.xml与我的title_fragment.xml

非常相似

我获得了一个forceClose!

我的logCat:

02-14 16:29:24.500: E/AndroidRuntime(5204): FATAL EXCEPTION: main
02-14 16:29:24.500: E/AndroidRuntime(5204): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.clicmobile.cannes/com.clicmobile.cannes.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.os.Looper.loop(Looper.java:130)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at java.lang.reflect.Method.invokeNative(Native Method)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at java.lang.reflect.Method.invoke(Method.java:507)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at dalvik.system.NativeStart.main(Native Method)
02-14 16:29:24.500: E/AndroidRuntime(5204): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.app.Activity.setContentView(Activity.java:1657)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at com.clicmobile.cannes.MainActivity.onCreate(MainActivity.java:13)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-14 16:29:24.500: E/AndroidRuntime(5204):     ... 11 more
02-14 16:29:24.500: E/AndroidRuntime(5204): Caused by: java.lang.NullPointerException
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:321)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:360)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:352)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at com.clicmobile.cannes.TitlesFragments.onCreateView(TitlesFragments.java:29)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:772)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:970)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1048)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:257)
02-14 16:29:24.500: E/AndroidRuntime(5204):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
02-14 16:29:24.500: E/AndroidRuntime(5204):     ... 20 more

1 个答案:

答案 0 :(得分:0)

您可能希望将类attrit添加到xml:

<fragment
    android:id="@+id/titles_frag"
    android:name="com.clicmobile.cannes.TitlesFragments"
    **android:class="com.clicmobile.cannes.TitlesFragments"**
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" 
>
</fragment>