填充类片段时出错:第10行xml

时间:2014-02-22 20:47:19

标签: java android xml fragment slidingpanelayout

我正在构建一个带有滑动窗格的应用程序,我无法运行它。我一直有FC。我是Android开发的新手。

package com.anshikka.datagushalpha;

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


public class MainActivity extends FragmentActivity {

SlidingPaneLayout pane;
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()) {
        getSupportFragmentManager().findFragmentById(R.id.leftpane).setHasOptionsMenu(false);
        getSupportFragmentManager().findFragmentById(R.id.rightpane).setHasOptionsMenu(true);
    }
}

public class PaneListener extends FragmentActivity implements SlidingPaneLayout.PanelSlideListener {
    public PaneListener()
    {}

    @Override
    public void onPanelClosed(View view) {
        System.out.println("Panel closed");

        getSupportFragmentManager().findFragmentById(R.id.leftpane);
        getSupportFragmentManager().findFragmentById(R.id.rightpane);
    }

    @Override
    public void onPanelOpened(View view) {
        System.out.println("Panel opened"); 
        getSupportFragmentManager().findFragmentById(R.id.leftpane).setHasOptionsMenu(true);
        getSupportFragmentManager().findFragmentById(R.id.rightpane).setHasOptionsMenu(false);
    }

    @Override
    public void onPanelSlide(View view, float arg1) {
        System.out.println("Panel sliding");
    }

}
}

这是我的XML文件

<?xml version="1.0" encoding="UTF-8"?>
<android.support.v4.widget.SlidingPaneLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/sp">



<!-- Left side pane. (Master) -->
<fragment android:id="@+id/leftpane" android:name="com.anshikka.datagushalpha.MainActivity"
    android:layout_width="190dp" android:layout_height="match_parent"
    class:com.
    android:layout_gravity="left" />


<!-- Right side page. (Slave) -->
<fragment android:id="@+id/rightpane" android:name="com.anshikka.datagushalpha.MainActivity"
    android:layout_width="350dp" android:layout_height="match_parent"
    android:layout_gravity="right" android:layout_weight="1" />

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

这是我的日志

02-22 14:27:01.624: E/AndroidRuntime(15203): FATAL EXCEPTION: main
02-22 14:27:01.624: E/AndroidRuntime(15203): Process: com.anshikka.datagushalpha, PID: 15203
02-22 14:27:01.624: E/AndroidRuntime(15203): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.anshikka.datagushalpha/com.anshikka.datagushalpha.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.os.Looper.loop(Looper.java:136)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at java.lang.reflect.Method.invoke(Native Method)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-22 14:27:01.624: E/AndroidRuntime(15203): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.app.Activity.setContentView(Activity.java:1929)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at com.anshikka.datagushalpha.MainActivity.onCreate(MainActivity.java:14)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.app.Activity.performCreate(Activity.java:5231)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-22 14:27:01.624: E/AndroidRuntime(15203):     ... 9 more
02-22 14:27:01.624: E/AndroidRuntime(15203): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.anshikka.datagushalpha.MainActivity that is not a Fragment
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.app.Fragment.instantiate(Fragment.java:585)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.app.Fragment.instantiate(Fragment.java:561)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.app.Activity.onCreateView(Activity.java:4778)
02-22 14:27:01.624: E/AndroidRuntime(15203):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
02-22 14:27:01.624: E/AndroidRuntime(15203):     ... 19 more
02-22 14:27:01.624: E/AndroidRuntime(15203): Caused by: java.lang.ClassCastException
02-22 14:27:01.624: E/AndroidRuntime(15203):     ... 23 more

2 个答案:

答案 0 :(得分:0)

您的XML布局文件看起来格格不入。只需删除class:com.行,看看它是否成功。

<fragment android:id="@+id/leftpane" android:name="com.anshikka.datagushalpha.MainActivity"
    android:layout_width="190dp" android:layout_height="match_parent"
    class:com. <===== THIS LINE IS UNNECESSARY
    android:layout_gravity="left" />

答案 1 :(得分:0)

我解决了这个问题,将正在解析的活动的全名添加到清单中,包括com。路径,如:

    <activity
        android:name="com.example.mainproject.PaneTest"

        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

之前是:

    <activity
        android:name=".PaneTest"

        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

希望这会有所帮助!!

这是一个很棒的代码: http://www.javaexperience.com/add-slidingpanelayout-to-your-android-application/