片段与android 2.3.7上的支持库

时间:2015-02-12 09:38:04

标签: android fragment android-support-library

我正在做我的应用程序,在我的手机上使用android 4.4.4工作正常但是当我在我的手机中运行android 2.3.7时出现错误。这是我的日志猫:

02-09 10:21:02.830: E/AndroidRuntime(1677): FATAL EXCEPTION: main
02-09 10:21:02.830: E/AndroidRuntime(1677): android.view.InflateException: Binary XML file line #58: Error inflating class <unknown>
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.view.LayoutInflater.createView(LayoutInflater.java:518)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at my.package.com.Noticias_Fragment.onCreateView(Noticias_Fragment.java:43)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.view.View.measure(View.java:8330)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.view.View.measure(View.java:8330)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.view.View.measure(View.java:8330)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
02-09 10:21:02.830: E/AndroidRuntime(1677):     at android

我正在使用Fragment,但支持库导入导入android.support.v4.app.Fragment。我片段的代码是:

import static com.nineoldandroids.view.ViewPropertyAnimator.animate;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


public class Noticias_Fragment extends Fragment {

    LinearLayout rl, container_leon;
    View v;

    int duration= 2000;
    LinearLayout menu_capa;
    Button button_;
    Button button_4, button1;
    LinearLayout container_2;
    String sonido;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {


        v= inflater.inflate(R.layout.noticias_caratula, container, false);
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        sonido= sharedPreferences.getString("sonido", null);

        rl=(LinearLayout)v.findViewById(R.id.container);
        container_2=(LinearLayout)v.findViewById(R.id.container_2);
        container_leon=(LinearLayout)v.findViewById(R.id.container_leon);
        menu_capa=(LinearLayout)v.findViewById(R.id.menu_capa);
        button_ = (Button) v.findViewById(R.id.button2);
        button1 = (Button) v.findViewById(R.id.button1);


        button_.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animate(menu_capa).alpha(0).setStartDelay(700).setDuration(900);
            }
        });

        Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Regular.ttf");
        //Typeface fontdos = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Light.ttf");

        TextView titulo_portada = (TextView) v.findViewById(R.id.titulo_portada);

        titulo_portada.setTypeface(font);

        button_4 = (Button) v.findViewById(R.id.button14);

        button_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                animate(rl).alpha(0).setStartDelay(1600).setDuration(600);

                new java.util.Timer().schedule( 
                    new java.util.TimerTask() {
                        @Override
                        public void run() {
                            if(sonido.equalsIgnoreCase("si")){
                               MediaPlayer mediaPlayer = MediaPlayer.create(getActivity(), R.raw.rugido);
                               mediaPlayer.start();
                           }
                       }
                   }, 
                   1650 
                   );
            }
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animate(container_leon).alpha(0).setStartDelay(2200).setDuration(600);
            }
        });


        container_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intenttresss = new Intent(getActivity(), FeedTabActivity.class);
                startActivity(intenttresss); 
            }
        });

        button_.performClick();
        button_4.performClick();
        button1.performClick();

        return v;
    }

}

,错误行是:

View v= inflater.inflate(R.layout.noticias_caratula, container, false);

我的noticias.xml文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@drawable/marijai"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#333"
        android:gravity="bottom"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/container_2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/marijaidifumi"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/titulo_portada"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/circle"
            android:text="Noticias"
            android:textColor="#fff"
            android:textSize="60sp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/container_leon"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/marijaidifumi"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:visibility="gone" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/marijai_sin"
        android:gravity="bottom"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:visibility="gone" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/menu_capa"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerVertical="true"
        android:background="@drawable/marijai"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:visibility="gone" />

    </LinearLayout>

</RelativeLayout>

并且错误的行是这样,但我看不出任何错误:

 <LinearLayout
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/marijai_sin"
        android:gravity="bottom"
        android:orientation="vertical" >

有人能帮助我吗?感谢。

1 个答案:

答案 0 :(得分:1)

片段在Android 3.0中引入。事实上,在Androids 3.0&lt;支持 libary 使用Fragments的系统实现。有关更多信息,请查看http://developer.android.com/guide/components/fragments.html

请检查此链接Fragments in Android 2.2.1, 2.3, 2.0. Is this possible?&amp; http://android-developers.blogspot.in/2011/02/android-30-fragments-api.html

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

        }

        @SuppressLint("InflateParams")
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View home_tab = inflater.inflate(R.layout.YOUR_LAYOUT, container, false);

return home_tab;
}