1

我正在为 android 开发 SlidingPaneLayout。我正在尝试在 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;
    }

}

主要的.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
4

2 回答 2

0

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

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

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

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

这篇文章有类似的问题,你可能会发现它有帮助

NullPointerException: 名称 == null

于 2013-11-13T09:09:58.157 回答
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

于 2013-11-13T09:05:15.120 回答