gpt4 book ai didi

Android App 长时间闲置后崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 07:56:37 25 4
gpt4 key购买 nike

我目前的手机遇到了一些非常奇怪的事情。如果我使用我正在开发的应用程序,我没有任何问题,直到我长时间停止使用它(我相信当这种情况发生时应用程序的实例被关闭)。我下载了一个“填满你的内存”的应用程序来模拟这种行为。我不明白的是错误:每次我在填充 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 中可能很重要,我用它来创建我的所有 Activity (我所有的 Activity 都扩展了 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 中。任何帮助将不胜感激。

最佳答案

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

您正在请求加载 ID 为 0 的资源,因为 mLayout 为 0。

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

坦率地说,恕我直言,将布局 ID 传递给 fragment 是一种代码味道,而传递 BaseActivity 是您代码中的一个彻头彻尾的错误。如果您的 fragment 需要一个 Activity ,请对该 fragment 调用 getActivity()。完全删除双参数构造函数。

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

我在 this sample project 中使用工厂方法/参数 Bundle 模式, 以便 fragment 知道它是 ViewPager 的哪个页面。该位置存储在参数Bundle中,并在 fragment 需要时从该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);
}
}

关于Android App 长时间闲置后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27128303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com