Android 行动吧夏洛克+;ListFragments+;碎片

Android 行动吧夏洛克+;ListFragments+;碎片,android,actionbarsherlock,Android,Actionbarsherlock,我对版本2.2中片段的兼容性有问题。我使用“夏洛克动作栏”来创建动作栏,但我不能创建两个片段。我一直都会遇到这个错误: 01-07 12:51:56.124: E/AndroidRuntime(475): FATAL EXCEPTION: main 01-07 12:51:56.124: E/AndroidRuntime(475): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example

我对版本2.2中片段的兼容性有问题。我使用“夏洛克动作栏”来创建动作栏,但我不能创建两个片段。我一直都会遇到这个错误:

 01-07 12:51:56.124: E/AndroidRuntime(475): FATAL EXCEPTION: main 01-07
 12:51:56.124: E/AndroidRuntime(475): java.lang.RuntimeException:
 Unable to start activity
 ComponentInfo{com.example.pruebasherlock_v4/com.example.pruebasherlock_v4.MainActivity}:
 android.view.InflateException: Binary XML file line #7: Error
 inflating class fragment 01-07 12:51:56.124: E/AndroidRuntime(475):
    at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 01-07 12:51:56.124: E/AndroidRuntime(475):     at
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
 01-07 12:51:56.124: E/AndroidRuntime(475):     at
 android.app.ActivityThread.access$2300(ActivityThread.java:125) 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
 01-07 12:51:56.124: E/AndroidRuntime(475):     at
 android.os.Handler.dispatchMessage(Handler.java:99) 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 android.os.Looper.loop(Looper.java:123) 01-07 12:51:56.124:
 E/AndroidRuntime(475):     at
 android.app.ActivityThread.main(ActivityThread.java:4627) 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 java.lang.reflect.Method.invokeNative(Native Method) 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 java.lang.reflect.Method.invoke(Method.java:521) 01-07 12:51:56.124:
 E/AndroidRuntime(475):     at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 01-07 12:51:56.124: E/AndroidRuntime(475):     at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 dalvik.system.NativeStart.main(Native Method) 01-07 12:51:56.124:
 E/AndroidRuntime(475): Caused by: android.view.InflateException:
 Binary XML file line #7: Error inflating class fragment 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
 01-07 12:51:56.124: E/AndroidRuntime(475):     at
 android.view.LayoutInflater.rInflate(LayoutInflater.java:618) 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 android.view.LayoutInflater.inflate(LayoutInflater.java:407) 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 android.view.LayoutInflater.inflate(LayoutInflater.java:320) 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 android.view.LayoutInflater.inflate(LayoutInflater.java:276) 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
 01-07 12:51:56.124: E/AndroidRuntime(475):     at
 android.app.Activity.setContentView(Activity.java:1647) 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 com.example.pruebasherlock_v4.MainActivity.onCreate(MainActivity.java:12)
 01-07 12:51:56.124: E/AndroidRuntime(475):     at
 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 01-07 12:51:56.124: E/AndroidRuntime(475):     at
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 01-07 12:51:56.124: E/AndroidRuntime(475):     ... 11 more 01-07
 12:51:56.124: E/AndroidRuntime(475): Caused by:
 java.lang.ClassNotFoundException: android.view.fragment in loader
 dalvik.system.PathClassLoader[/data/app/com.example.pruebasherlock_v4-1.apk]
 01-07 12:51:56.124: E/AndroidRuntime(475):     at
 dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
 01-07 12:51:56.124: E/AndroidRuntime(475):     at
 java.lang.ClassLoader.loadClass(ClassLoader.java:573) 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 java.lang.ClassLoader.loadClass(ClassLoader.java:532) 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 android.view.LayoutInflater.createView(LayoutInflater.java:466) 01-07
 12:51:56.124: E/AndroidRuntime(475):   at
 android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
 01-07 12:51:56.124: E/AndroidRuntime(475):     at
 com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
 01-07 12:51:56.124: E/AndroidRuntime(475):     at
 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
 01-07 12:51:56.124: E/AndroidRuntime(475):     ... 20 more
我的主要活动包括:

package com.example.pruebasherlock_v4;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <fragment 
        android:id="@+id/listaIzq"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="fill_parent" 
        class="com.example.pruebasherlock_v4.Lista"/>

    <fragment 
        android:id="@+id/detallesDer"
        android:layout_weight="3"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        class="com.example.pruebasherlock_v4.Detalle" />


</LinearLayout>
My main.xml包含以下内容:

package com.example.pruebasherlock_v4;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <fragment 
        android:id="@+id/listaIzq"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="fill_parent" 
        class="com.example.pruebasherlock_v4.Lista"/>

    <fragment 
        android:id="@+id/detallesDer"
        android:layout_weight="3"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        class="com.example.pruebasherlock_v4.Detalle" />


</LinearLayout>
我觉得放了很多代码,但不是因为它错了

非常感谢你的帮助

我的主要活动包括:

package com.example.pruebasherlock_v4;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <fragment 
        android:id="@+id/listaIzq"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="fill_parent" 
        class="com.example.pruebasherlock_v4.Lista"/>

    <fragment 
        android:id="@+id/detallesDer"
        android:layout_weight="3"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        class="com.example.pruebasherlock_v4.Detalle" />


</LinearLayout>
如果您打算使用ActionBarSherlock,请从一个
Sherlock
类继承,例如
SherlockActivity
。如果您还打算使用片段,请从
SherlockFragmentActivity
继承

引述:

在所有版本的Android上创建使用操作栏的活动时,必须声明活动以扩展以“Sherlock”开头的任何活动类(例如SherlockActivity、SherlockFragmentActivity)


为了使用compability碎片和ABS,您必须扩展SherlockFragmentActivity

public class MainActivity extends SherlockFragmentActivity {

非常感谢。从SherlockActivity扩展之后我失败了,但扩展SherlockFragmentActivity工作得很完美。我不确定您测试的是什么设备,但对于运行<4.0的设备,我建议对片段使用Support library XML小部件,而不是android.Support.v4.app.Fragment的本机片段,而不是
Fragment
@jsimon:如果您使用的是ActionBarSherlock,就像OP一样,您必须从片段类的
Sherlock
版本继承,正如OP所做的那样。@commonware甚至在布局XML中?@jsimon:layout XML中的
元素必须引用从片段类的
Sherlock
版本继承的类,如果该片段将参与操作栏。ActionBarSherlock可以使用
android.support.v4.app.Fragment
类——有关示例,请参见Maps V2中的
SupportMapFragment
。但是,如果您想让片段向操作栏添加项目、启动操作模式或以其他方式使用操作栏,您需要从
SherlockFragment
SherlockListFragment
等继承。如果您使用的是ActionBarSherlock。谢谢!从SherlockActivity扩展之后我失败了,但扩展SherlockFragmentActivity工作得很完美。