Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从xml扩展类片段时出错_Android_Android Fragments_Android Xml - Fatal编程技术网

Android 从xml扩展类片段时出错

Android 从xml扩展类片段时出错,android,android-fragments,android-xml,Android,Android Fragments,Android Xml,我在这里发现了一些类似的问题,但建议的答案不适合我 因此,错误是: 08-13 14:40:10.723: E/AndroidRuntime(630): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragments/com.example.fragments.MainActivity}: android.view.InflateException: Binary XML file

我在这里发现了一些类似的问题,但建议的答案不适合我

因此,错误是:

08-13 14:40:10.723: E/AndroidRuntime(630): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragments/com.example.fragments.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
我的主要活动是:

    package com.example.fragments;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

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

}
我的xml是:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/listFragment"
        android:layout_width="150dip"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize"
        class="com.example.fragments.ListFragment" >
    </fragment>

    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.example.fragments.DetailFragment" >
    </fragment>

</LinearLayout>

非常感谢您在这个问题上提供的任何帮助


xx

我想这个错误可能是由于

android:layout_marginTop="?android:attr/actionBarSize"

actionBarSize是从api 11开始引入的,当您使用支持库时,我假设您是在Android 3.0之前的设备上开发的。尝试删除该属性,看看是否有问题。

我认为错误可能是由于

android:layout_marginTop="?android:attr/actionBarSize"

actionBarSize是从api 11开始引入的,当您使用支持库时,我假设您是在Android 3.0之前的设备上开发的。尝试删除该属性,看看是否是问题。

因此,通过在项目中实现所有与片段相关的导入,基本上解决了问题,如下所示:

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment
而不是:

import android.app.Fragment
import android.app.FragmentActivity
import android.app.ListFragment

因此,通过在项目中实现所有与片段相关的导入,基本上解决了问题,如下所示:

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment
而不是:

import android.app.Fragment
import android.app.FragmentActivity
import android.app.ListFragment

类com.example.fragments.ListFragment、com.example.fragments.DetailFragment是否存在于您的应用程序中?亲爱的nandeesh,是的,它们存在于项目文件夹中,并且在包com.example.fragments中。类com.example.fragments.ListFragment、com.example.fragments.DetailFragment是否存在于您的应用程序中?亲爱的nandeesh,是的,它们存在于项目文件夹中,并且在包com.example.fragments我是Android新手,但是Android.support.v4.app包中的类不是新Android功能的后台端口,所以你可以在旧版本的Android上使用新功能?如果更改导入有效,它也有效。但是我仍然很好奇为什么android.app的实现不能被夸大。你的问题是100%公平的,但老实说——我不知道:)一种方法对我有效,另一种方法不行。就这么简单:)那时我没有足够的时间来讨论细节。抱歉,原因是您的活动扩展了android.support.v4.app.FragmentActivity。android.support.v4.app和android.app是独立的类树,因此其中一个中的片段可能看起来像另一个中的片段,但它们是独立的对象。若您检查它们的继承层次结构,它们会在对象之后发散。只有在需要时才使用支持库,这样你才能避免这个问题。我是Android新手,但Android.support.v4.app包中的类不是新Android功能的后端口,所以你可以在旧版本的Android上使用新功能?如果更改导入有效,它也有效。但是我仍然很好奇为什么android.app的实现不能被夸大。你的问题是100%公平的,但老实说——我不知道:)一种方法对我有效,另一种方法不行。就这么简单:)那时我没有足够的时间来讨论细节。抱歉,原因是您的活动扩展了android.support.v4.app.FragmentActivity。android.support.v4.app和android.app是独立的类树,因此其中一个中的片段可能看起来像另一个中的片段,但它们是独立的对象。若您检查它们的继承层次结构,它们会在对象之后发散。仅在需要时使用支持库,这样可以避免此问题。