1

使用我的应用程序后,我正在按 Home 以在后台运行应用程序。如果我立即再次打开该应用程序,它会打开该应用程序。

但是,如果我在某个时间后打开应用程序意味着距离最近的示例 15 分钟,它的强制关闭应用程序并使用以下 logcat 信息

01-24 11:04:37.311: I/dalvikvm(6418): Turning on JNI app bug workarounds for target SDK version 8...
01-24 11:04:37.639: W/dalvikvm(6418): threadid=1: thread exiting with uncaught exception (group=0x41c71930)
01-24 11:04:37.654: E/AndroidRuntime(6418): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.roseenvy.ui.activities/com.roseenvy.ui.activities.AppHomeScreen}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.os.Looper.loop(Looper.java:137)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-24 11:04:37.654: E/AndroidRuntime(6418): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-24 11:04:37.654: E/AndroidRuntime(6418): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-24 11:04:37.654: E/AndroidRuntime(6418): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-24 11:04:37.654: E/AndroidRuntime(6418): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.app.Activity.setContentView(Activity.java:1881)
01-24 11:04:37.654: E/AndroidRuntime(6418): at com.roseenvy.ui.activities.AppHomeScreen.onCreate(AppHomeScreen.java:34)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.app.Activity.performCreate(Activity.java:5104)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-24 11:04:37.654: E/AndroidRuntime(6418): at com.roseenvy.ui.fragments.Special.onCreateView(Special.java:42)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:846)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1160)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
01-24 11:04:37.654: E/AndroidRuntime(6418): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)

这只发生在我的 google nexus 中。我在 LG optimus 和 google tab2 中测试了该应用程序,也在这些设备应用程序上正常工作。

我的活动布局是

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pageflip"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white" >

    <fragment
        android:name="com.roseenvy.ui.fragments.Special"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
     />

    <fragment
        android:name="com.roseenvy.ui.fragments.CreateMyOwnViewPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@+id/fragcreatehome"
      />

    <fragment
        android:id="@+id/previewfrag"
        android:name="com.roseenvy.ui.fragments.PreviewFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <fragment
        android:id="@+id/addmessagefrag"
        android:name="com.roseenvy.ui.fragments.AddMessage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <fragment
        android:name="com.roseenvy.ui.fragments.BuyNow"
        android:id="@+id/buynowfrag"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <fragment
        android:name="com.roseenvy.ui.fragments.ConfirmOrder"
        android:layout_width="fill_parent"
        android:id="@+id/confirmfrag"
        android:layout_height="fill_parent" />

</ViewFlipper>

这是我的片段布局

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

    <ImageView
        android:id="@+id/i_spf_spl_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY" />

    <RelativeLayout
        android:id="@+id/specialLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ImageView
            android:id="@+id/i_spf_transp_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:src="@drawable/transp_logo" />

        <LinearLayout
            android:id="@+id/spf_spl_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/this_weeks_special" />

            <com.roseenvy.ui.customviews.RoseTextView
                android:id="@+id/specialPrice"
                style="@style/SpecialText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:background="@drawable/price_bkg"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="45sp" />

            <com.roseenvy.ui.customviews.RoseTextView
                android:id="@+id/specialTitle"
                style="@style/SpecialText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:singleLine="true"
                android:text="@string/title"
                android:textColor="@color/white"
                android:textSize="50sp" />

            <LinearLayout
                android:id="@+id/gospecial"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:orientation="horizontal" >

                <com.roseenvy.ui.customviews.RoseTextView
                    android:id="@+id/t_spf_spl_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/see_special_offer"
                    android:textColor="@color/white"
                    android:textSize="19sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="5dp"
                    android:src="@drawable/spl_offer_arrow" />
            </LinearLayout>
        </LinearLayout>

        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@drawable/create_your_own" >

            <LinearLayout
                android:id="@+id/createownbottom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:orientation="vertical"
                android:paddingTop="7dp" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/down_arrow" />

                <com.roseenvy.ui.customviews.RoseTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:text="@string/createyourown"
                    android:textColor="@color/white"
                    android:textSize="14sp" />
            </LinearLayout>
        </FrameLayout>
    </RelativeLayout>

</FrameLayout>

活动代码

public class AppHomeScreen extends FragmentActivity {

    public static ViewFlipper pageFlip;

    private Utils utils;

    public AppPreferences appPreferences;
    @Override
    protected void onCreate(Bundle bundle) {

        super.onCreate(bundle);
        setContentView(R.layout.apphome);
        pageFlip = (ViewFlipper) findViewById(R.id.pageflip);
        appPreferences = new AppPreferences(this);
        utils = new Utils();
    }
}

片段代码

public class Special extends BaseFragment implements OnClickListener {
    private ImageView specialBg;
    private RoseTextView title, price;
    public Bitmap bg;
    private LinearLayout createownbottom, gospecial;
    private SpecialProduct specialProduct;
    private String roseColor;
    public static boolean special = false;

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

        View v = inflater.inflate(R.layout.special_fragment, container, false);
        // pageFlip = AppHomeScreen.pageFlip;
        specialBg = (ImageView) v.findViewById(R.id.i_spf_spl_image);
        ImageDownloader imageLoader = new ImageDownloader(getActivity(), false);
        imageLoader.setMode(Mode.CORRECT);

        specialProduct = ApplicationData.getSpecialProduct();
        imageLoader.download(specialProduct.getSpecialImage(), specialBg, null);

        roseColor = specialProduct.getRoseInfo().getColor();
        title = (RoseTextView) v.findViewById(R.id.specialTitle);
        title.setText(specialProduct.getTitle().toUpperCase());
        price = (RoseTextView) v.findViewById(R.id.specialPrice);
        price.setText(getString(R.string._) + specialProduct.getPrice());
        price.setTextColor(Color.parseColor("#" + roseColor));
        createownbottom = (LinearLayout) v.findViewById(R.id.createownbottom);
        createownbottom.setOnClickListener(this);
        gospecial = (LinearLayout) v.findViewById(R.id.gospecial);
        gospecial.setOnClickListener(this);
        return v;
    }
}
4

1 回答 1

0

我可能是错的,但似乎当您从“最近”启动该应用程序时,它会调用一个视图,该视图由于Activity在后台时间过长而不再膨胀,因此被杀死。

这发生在其他设备上而不是在 Google Nexus 上这一事实并不能说明太多——当 Android 终止后台进程时可能是任意的,具体取决于您的内存使用情况和其他因素。如文档中所述:

Android 系统尝试尽可能长时间地保留应用程序进程,但最终需要在内存不足时删除旧进程。如活动生命周期中所述,关于删除哪个进程的决定与用户与其交互的状态密切相关。通常,根据其中运行的活动,流程可以处于四种状态,此处按重要性顺序列出。系统会在杀死更重要的进程(第一个)之前杀死不太重要的进程(最后一个)。

您是否尝试在方法中保存您的应用程序的状态,或者在、、或onStop()中的任何一个中重新填充上述日志中描述的组件?这完全取决于您将使用哪种方法。供参考:http: //developer.android.com/reference/android/app/Activity.html#ActivityLifecycleonCreate()onStart()onResume()onRestart()

于 2013-01-25T05:30:44.207 回答