Android应用程序在闲置很长时间后崩溃了

时间:2014-11-25 13:41:10

标签: android android-layout android-activity crash android-resources

我目前正面临着一些与我的手机非常奇怪的事情。如果我使用我正在开发的应用程序,我没有问题,直到我停止使用它很长一段时间(当这发生时,应用程序的实例关闭我相信)。我下载了一个“填充你的RAM”的应用程序,以模仿这种行为。我不明白的是错误:每次我在填充RAM后尝试使用应用程序时,我都会得到一个ResourceNotFound异常,这对我来说至少没有意义。正如您在跟踪中看到的那样,错误位于以下行:

mView = inflater.inflate(mLayout,container, false);

这是追踪:

11-25 08:22:09.496: E/AndroidRuntime(6386): FATAL EXCEPTION: main
11-25 08:22:09.496: E/AndroidRuntime(6386): Process: com.papinotas, PID: 6386
11-25 08:22:09.496: E/AndroidRuntime(6386): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.papinotas/com.papinotas.views.activities.TeacherMainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.app.ActivityThread.access$900(ActivityThread.java:161)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.os.Looper.loop(Looper.java:157)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.app.ActivityThread.main(ActivityThread.java:5356)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at java.lang.reflect.Method.invokeNative(Native Method)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at java.lang.reflect.Method.invoke(Method.java:515)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at dalvik.system.NativeStart.main(Native Method)
11-25 08:22:09.496: E/AndroidRuntime(6386): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.content.res.Resources.getValue(Resources.java:2036)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:3265)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.content.res.Resources.getLayout(Resources.java:1852)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at com.androidinit.base.PlaceHolderFragment.onCreateView(PlaceHolderFragment.java:29)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.app.Fragment.performCreateView(Fragment.java:1700)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1853)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.app.Activity.performCreate(Activity.java:5429)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-25 08:22:09.496: E/AndroidRuntime(6386):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
11-25 08:22:09.496: E/AndroidRuntime(6386):     ... 11 more
11-25 08:22:09.506: W/ActivityManager(847):   Force finishing activity com.papinotas/.views.activities.TeacherMainActivity
11-25 08:22:09.516: W/ContextImpl(847): Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1479 com.android.server.am.ActivityStack.startPausingLocked:1006 com.android.server.am.ActivityStack.finishActivityLocked:3162 com.android.server.am.ActivityStack.finishTopRunningActivityLocked:3005 com.android.server.am.ActivityStackSupervisor.finishTopRunningActivityLocked:3142 
11-25 08:22:09.526: D/CrashAnrDetector(847): processName: com.papinotas
11-25 08:22:09.526: D/CrashAnrDetector(847): broadcastEvent : com.papinotas data_app_crash
11-25 08:22:09.526: V/SmartFaceService - 3rd party pause(847): onReceive [android.intent.action.ACTIVITY_STATE/com.papinotas/pause]
11-25 08:22:09.526: W/ContextImpl(847): Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1479 com.android.server.analytics.data.collection.application.CrashAnrDetector.broadcastEvent:296 com.android.server.analytics.data.collection.application.CrashAnrDetector.processDropBoxEntry:254 com.android.server.analytics.data.collection.application.CrashAnrDetector.access$100:60 com.android.server.analytics.data.collection.application.CrashAnrDetector$1.onReceive:102 

以下是完整的跟踪类:

package com.androidinit.base;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

    public class PlaceHolderFragment extends Fragment {

    private int mLayout;
    private View mView;
    private BaseActivity mActivity;

    public PlaceHolderFragment() {
    }

    public PlaceHolderFragment(int mLayout, BaseActivity mActivity) {

        this.mLayout = mLayout;
        this.mActivity = mActivity;

    }

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

            mView = inflater.inflate(mLayout,container, false);
            mActivity.setActivityView(mView);

        return mView;
    }

}

此外,我认为这些方法在BaseActivity中可能很重要,我用它来创建我的所有活动(我的所有活动都扩展了BaseActivity)

public class BaseActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        session = new SessionManager(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ai_base_activity);
        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
    }
    @Override
    public void onResume(){
        super.onResume();
        setSpinner();
    }
    public void createFragmentView(int layoutId) {

        //FIXME agregar la clase hija
        mFragment = new PlaceHolderFragment(layoutId, this);
        getFragmentManager().beginTransaction().add(R.id.container, mFragment).commit();
    }

    public PlaceHolderFragment getPlaceHolderFragment( ) {
        return mFragment;
    }
}

我还没有找到解决这个问题的方法,这个问题出现在我的Galaxy S4,Android 4.4.3中。 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

你的 mLayout 获取默认值,当片段被销毁并重新创建时,该值为0。当您将应用程序更改为该RAM填充应用程序并返回到您的应用程序时,就会发生这种情况。

所以你需要做的是使用onSavedInstanceState方法并将你需要的变量存储在那里。当调用onCreateView并且参数 savedInstanceState 不为null时,您必须从Bundle中检索变量。

阅读本文以了解我想说的内容:onSavedInstanceState method

然后它会是这样的:

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

    if (savedInstanceState != null) {
        mLayout = savedInstanceState.getInt("layout_id");
    }

    mView = inflater.inflate(mLayout,container, false);
    // Return the Activity this fragment is currently associated with.
    BaseActivity activity = (BaseActivity) getActivity();
    activity.setActivityView(mView);

    return mView;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putInt("layout_id", mLayout);
    super.onSaveInstanceState(outState);
}

答案 1 :(得分:1)

您收到ResourceNotFoundException,因为您要求加载ID为0的资源。

您要求加载ID为0的资源,因为mLayout为0。

mLayout为0,因为它是您的片段的数据成员,并且您的片段正在重新创建,可能是因为您的进程在后台时终止。

坦率地说,恕我直言,将布局ID传递给片段是一种代码气味,并且传递BaseActivity是代码中的一个完全错误。如果您的片段需要活动,请在片段上调用getActivity()。完全删除双参数构造函数。

假设实际上有合理的理由传递布局ID,请使用工厂方法模式和参数Bundle,而不是您的双参数构造函数。参数Bundle会自动保存为片段实例状态的一部分,因此当您的进程终止并稍后重新创建时,它不会丢失。

我在this sample project中使用工厂方法/参数Bundle模式,以便片段知道它是ViewPager的哪个页面。 position存储在参数Bundle中,并在片段需要时从Bundle检索:

/***
  Copyright (c) 2012-14 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.pager;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class EditorFragment extends Fragment {
  private static final String KEY_POSITION="position";

  static EditorFragment newInstance(int position) {
    EditorFragment frag=new EditorFragment();
    Bundle args=new Bundle();

    args.putInt(KEY_POSITION, position);
    frag.setArguments(args);

    return(frag);
  }

  @Override
  public View onCreateView(LayoutInflater inflater,
                           ViewGroup container,
                           Bundle savedInstanceState) {
    View result=inflater.inflate(R.layout.editor, container, false);
    EditText editor=(EditText)result.findViewById(R.id.editor);
    int position=getArguments().getInt(KEY_POSITION, -1);

    editor.setHint(String.format(getString(R.string.hint), position + 1));

    return(result);
  }
}