android 2.3.3充气错误SlidePaneLayout

时间:2013-11-13 08:58:29

标签: android android-layout slidingpanelayout

我正在使用SlidingPaneLayout for android。我试图在android 2.3.3中实现它。但问题是它在膨胀时会产生问题。我按照教程进行了操作。它的minSdk为10.如果我尝试使用以下版本实现相同的。它给出了错误。代码是这样的。

package com.example.slidpanelayout;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SlidingPaneLayout;
import android.view.Menu;
import android.view.View;

public class MainActivity extends FragmentActivity {

    SlidingPaneLayout Pane;

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

        Pane = (SlidingPaneLayout) findViewById(R.id.sp);

        Pane.setPanelSlideListener(new PaneListener());

        if(!Pane.isSlideable()){
            getFragmentManager().findFragmentById(R.id.leftpane).setHasOptionsMenu(false);
            getFragmentManager().findFragmentById(R.id.rightpane).setHasOptionsMenu(true);  
        }
    }

    private class PaneListener implements SlidingPaneLayout.PanelSlideListener{

        @Override
        public void onPanelClosed(View arg0) {
            // TODO Auto-generated method stub
            System.out.println("Panel closed");

            getFragmentManager().findFragmentById(R.id.leftpane).setHasOptionsMenu(false);
            getFragmentManager().findFragmentById(R.id.rightpane).setHasOptionsMenu(true);
        }

        @Override
        public void onPanelOpened(View arg0) {
            // TODO Auto-generated method stub
            System.out.println("Panel Opened");
            getFragmentManager().findFragmentById(R.id.leftpane).setHasOptionsMenu(true);
            getFragmentManager().findFragmentById(R.id.rightpane).setHasOptionsMenu(false);
        }

        @Override
        public void onPanelSlide(View arg0, float arg1) {
            // TODO Auto-generated method stub
            System.out.println("Sliding");
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

main.xml中

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

    <fragment 
        android:id="@+id/leftpane"
        android:layout_width="190dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"/>
    <fragment 
        android:id="@+id/rightpane"
        android:layout_width="350dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:layout_weight="1"/>
</android.support.v4.widget.SlidingPaneLayout>

我尝试了互联网上提供的所有建议。请帮帮我。日志是

11-13 02:30:47.439: E/AndroidRuntime(1597): FATAL EXCEPTION: main
11-13 02:30:47.439: E/AndroidRuntime(1597): Process: com.example.slidpanelayout, PID: 1597
11-13 02:30:47.439: E/AndroidRuntime(1597): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.slidpanelayout/com.example.slidpanelayout.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.os.Looper.loop(Looper.java:137)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at java.lang.reflect.Method.invoke(Method.java:515)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at dalvik.system.NativeStart.main(Native Method)
11-13 02:30:47.439: E/AndroidRuntime(1597): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.app.Activity.setContentView(Activity.java:1928)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at com.example.slidpanelayout.MainActivity.onCreate(MainActivity.java:16)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.app.Activity.performCreate(Activity.java:5243)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-13 02:30:47.439: E/AndroidRuntime(1597):     ... 11 more
11-13 02:30:47.439: E/AndroidRuntime(1597): Caused by: java.lang.NullPointerException: name == null
11-13 02:30:47.439: E/AndroidRuntime(1597):     at java.lang.VMClassLoader.findLoadedClass(Native Method)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:350)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at java.lang.ClassLoader.loadClass(ClassLoader.java:487)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:436)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:255)
11-13 02:30:47.439: E/AndroidRuntime(1597):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
11-13 02:30:47.439: E/AndroidRuntime(1597):     ... 21 more

2 个答案:

答案 0 :(得分:0)

您需要提供片段类。

例如:

<fragment class="com.example.android.apis.app.FragmentArguments$MyFragment"
    android:id="@+id/embedded"
    android:layout_width="0px" android:layout_height="wrap_content"
    android:layout_weight="1"
    android:label="@string/fragment_arguments_embedded" />

http://developer.android.com/reference/android/support/v4/app/Fragment.html

答案 1 :(得分:0)

添加片段时,除了id之外,还应为其指定名称。这就是您收到错误消息name == null

的原因

android:name属性指定要在布局中实例化的Fragment类。

在此处查看开发人员指南:

  

http://developer.android.com/guide/components/fragments.html#Adding

此帖有类似问题,您可能会觉得有用

  

NullPointerException: name == null