SlidingPaneLayout和Fragment $ InstantiationException

时间:2014-03-07 13:50:44

标签: java android xml

我想把SlidingPaneLayout实现到我的应用程序中。以下各种教程我写了这段代码。在activity_main布局中

<android.support.v4.widget.SlidingPaneLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/sliding_pane_layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

    <fragment
       android:id="@+id/list"
       class="com.mypackage.name.FirstFragment"
       android:layout_width="280dp"
       android:layout_height="match_parent"
       android:layout_gravity="left"></fragment>

    <fragment
       android:id="@+id/content"
       class="com.mypackage.name.SecondFragment"
       android:layout_width="450dp"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:paddingLeft="16dp"
       android:paddingRight="16dp"></fragment>

</android.support.v4.widget.SlidingPaneLayout>

第一个片段的布局是

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

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

表示第二个片段

<?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/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

课程主要活动

public class MainActivity extends Activity {

    SlidingPaneLayout slidingLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        slidingLayout = (SlidingPaneLayout)findViewById(R.id.sliding_pane_layout);
        slidingLayout.setPanelSlideListener(new SlidingPaneLayoutListener());
    }

    private class SlidingPaneLayoutListener implements SlidingPaneLayout.PanelSlideListener {

        @Override
        public void onPanelClosed(View view) {
            // TODO Auto-generated method stub
            getFragmentManager().findFragmentById(R.id.list).setHasOptionsMenu(false);
            getFragmentManager().findFragmentById(R.id.content).setHasOptionsMenu(true);
        }

        @Override
        public void onPanelOpened(View view) {
            // TODO Auto-generated method stub
            getFragmentManager().findFragmentById(R.id.list).setHasOptionsMenu(false);
            getFragmentManager().findFragmentById(R.id.content).setHasOptionsMenu(true);
        }

        @Override
        public void onPanelSlide(View view, float arg1) {
            // TODO Auto-generated method stub

        }

    }

}

第一个片段类

public class FirstFragment extends Fragment {

    ArrayAdapter<String> adapter;
    String[] items;
    ListView list;
    View view;

    public View onCreateView(LayoutInflater inflater, Bundle savedIstanceState, ViewGroup container) {
        view = inflater.inflate(R.layout.lista_fragment, container, false);
        list = (ListView)view.findViewById(R.id.listView);
        items = new String[] {"EXAMPLE", "EXAMPLE", "EXAMPLE"};
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
        list.setAdapter(adapter);

        return view;
    }

}

当我启动此应用程序崩溃时.. logcat:

03-07 14:42:49.501: E/AndroidRuntime(28780): FATAL EXCEPTION: main
03-07 14:42:49.501: E/AndroidRuntime(28780): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage.name/com.mypackage.name.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.os.Looper.loop(Looper.java:136)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.app.ActivityThread.main(ActivityThread.java:5081)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at java.lang.reflect.Method.invokeNative(Native Method)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at java.lang.reflect.Method.invoke(Method.java:515)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at dalvik.system.NativeStart.main(Native Method)
03-07 14:42:49.501: E/AndroidRuntime(28780): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.app.Activity.setContentView(Activity.java:1929)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at com.mypackage.name.MainActivity.onCreate(MainActivity.java:15)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.app.Activity.performCreate(Activity.java:5231)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
03-07 14:42:49.501: E/AndroidRuntime(28780):    ... 11 more
03-07 14:42:49.501: E/AndroidRuntime(28780): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.mypackage.name.FirstFragment that is not a Fragment
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.app.Fragment.instantiate(Fragment.java:585)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.app.Fragment.instantiate(Fragment.java:561)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.app.Activity.onCreateView(Activity.java:4778)
03-07 14:42:49.501: E/AndroidRuntime(28780):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
03-07 14:42:49.501: E/AndroidRuntime(28780):    ... 21 more
03-07 14:42:49.501: E/AndroidRuntime(28780): Caused by: java.lang.ClassCastException
03-07 14:42:49.501: E/AndroidRuntime(28780):    ... 25 more

我也尝试过清理项目,但我也有同样的错误。问题出在哪里?

1 个答案:

答案 0 :(得分:0)

在XML布局中只需编写Fragment而不是fragment。 XML区分大小写。它由日志中的#7: Error inflating class fragment部分显示。

如果您打算使用FirstFragment类,那么在声明XML文件中的元素(包括包名称)时,您必须编写完整名称:

<com.mypackage.name.FirstFragment
    ... >
    ...
</com.mypackage.name.FirstFragment>