Android,无法实现SlidingPaneLayout,在setContentView失败

时间:2014-05-21 00:39:50

标签: android slidingpanelayout

我已经问过了,但是没有人回答,所以我再问一遍。我正在尝试从android.support.v4.widget实现SlidingPaneLayout。我正在尝试实现最可能的准系统代码。任何事情都会加载的唯一方法是我注释掉setContentView()。代码无法更简单,甚至不会在没有错误的情况下运行。任何帮助都将非常感激。

MainActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SlidingPaneLayout;


public class MainActivity extends Activity {

    SlidingPaneLayout paneLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.root_layout);

        /*
        paneLayout = (SlidingPaneLayout) findViewById(R.id.root_layout);
        paneLayout.setPanelSlideListener(new SlidingPaneLayout.SimplePanelSlideListener() {
            @Override
            public void onPanelSlide(View panel, float slideOffset) {
                super.onPanelSlide(panel, slideOffset);
            }

            @Override
            public void onPanelOpened(View panel) {
                super.onPanelOpened(panel);
            }

            @Override
            public void onPanelClosed(View panel) {
                super.onPanelClosed(panel);
            }
        });
        */
    }
}

主XML文件:

<?xml version="1.0" encoding="utf-8"?>

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

    <!--fragment structure follows-->
    <fragment
        android:id="@+id/content_pane_fragment"
        android:name="com.sonfluence.app.ContentPane"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        </fragment>
    <fragment
        android:id="@+id/menu_pane_fragment"
        android:name="com.sonfluence.app.MenuPane"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </fragment>

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

内容片段:

package com.sonfluence.app;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by c on 5/20/14.
 */
public class ContentPane extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.content_pane, container, false);
        return v;
    }
}

内容XML(我也尝试将其作为xml中的片段进行投射,没有):

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content_pane">

</RelativeLayout>

Logcat:

05-20 17:30:29.312  20648-20648/com.sonfluence.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.sonfluence.app, PID: 20648
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sonfluence.app/com.sonfluence.app.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.access$800(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
            at android.app.Activity.setContentView(Activity.java)
            at com.sonfluence.app.MainActivity.onCreate(MainActivity.java:15)
            at android.app.Activity.performCreate(Activity.java)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.access$800(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.sonfluence.app.ContentPane that is not a Fragment
            at android.app.Fragment.instantiate(Fragment.java)
            at android.app.Fragment.instantiate(Fragment.java)
            at android.app.Activity.onCreateView(Activity.java)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
            at android.app.Activity.setContentView(Activity.java)
            at com.sonfluence.app.MainActivity.onCreate(MainActivity.java:15)
            at android.app.Activity.performCreate(Activity.java)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.access$800(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException
            at android.app.Fragment.instantiate(Fragment.java)
            at android.app.Fragment.instantiate(Fragment.java)
            at android.app.Activity.onCreateView(Activity.java)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
            at android.app.Activity.setContentView(Activity.java)
            at com.sonfluence.app.MainActivity.onCreate(MainActivity.java:15)
            at android.app.Activity.performCreate(Activity.java)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.access$800(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

如果您使用的是android.support.v4.app.Fragment,则需要延长FragmentActivity - 它会为您ClassCastException提供android.app.Fragment,因为Activity为{{1}} }不知道Fragments的支持版本。)