在我的应用程序中,在应用程序的每个开始,我都想执行PHP脚本并相应地加载应用程序。因此,我创建了一个静态布尔值
boolean firstRun = false;
在我的Onstart中,我添加了以下代码:
// If images need to be downloaded, call SProgressAsyncTask class
public void onStart() {
super.onStart();
if (firstRun) {
sat = new SProgressAsyncTask(this);
sat.execute("");
firstRun = false;
}
}
在索尼的朋友的设备上,它可以完美地工作,即以上代码仅执行一次,即启动应用程序。在我的Sony Xperia P上,每次应用程序都进入此主屏幕时,上述代码都会执行。当将firstrun变成错误时,为什么上述代码每次返回此活动时都会运行。我尝试在设备上调试,并收到以下错误:
10-22 17:41:49.879: E/WindowManager(30235): Activity org.mumbai77.core.Mumbai77Activity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4143a460 that was originally added here
10-22 17:41:49.879: E/WindowManager(30235): android.view.WindowLeaked: Activity org.mumbai77.core.Mumbai77Activity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4143a460 that was originally added here
10-22 17:41:49.879: E/WindowManager(30235): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
10-22 17:41:49.879: E/WindowManager(30235): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
10-22 17:41:49.879: E/WindowManager(30235): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
10-22 17:41:49.879: E/WindowManager(30235): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
10-22 17:41:49.879: E/WindowManager(30235): at android.view.Window$LocalWindowManager.addView(Window.java:537)
10-22 17:41:49.879: E/WindowManager(30235): at android.app.Dialog.show(Dialog.java:278)
10-22 17:41:49.879: E/WindowManager(30235): at org.mumbai77.components.ProgressAsyncTask.onPreExecute(ProgressAsyncTask.java:56)
10-22 17:41:49.879: E/WindowManager(30235): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
10-22 17:41:49.879: E/WindowManager(30235): at android.os.AsyncTask.execute(AsyncTask.java:511)
10-22 17:41:49.879: E/WindowManager(30235): at org.mumbai77.core.Mumbai77Activity.onStart(Mumbai77Activity.java:170)
10-22 17:41:49.879: E/WindowManager(30235): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
10-22 17:41:49.879: E/WindowManager(30235): at android.app.Activity.performStart(Activity.java:4475)
10-22 17:41:49.879: E/WindowManager(30235): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1940)
10-22 17:41:49.879: E/WindowManager(30235): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
10-22 17:41:49.879: E/WindowManager(30235): at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-22 17:41:49.879: E/WindowManager(30235): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
10-22 17:41:49.879: E/WindowManager(30235): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 17:41:49.879: E/WindowManager(30235): at android.os.Looper.loop(Looper.java:137)
10-22 17:41:49.879: E/WindowManager(30235): at android.app.ActivityThread.main(ActivityThread.java:4441)
10-22 17:41:49.879: E/WindowManager(30235): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 17:41:49.879: E/WindowManager(30235): at java.lang.reflect.Method.invoke(Method.java:511)
10-22 17:41:49.879: E/WindowManager(30235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-22 17:41:49.879: E/WindowManager(30235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-22 17:41:49.879: E/WindowManager(30235): at dalvik.system.NativeStart.main(Native Method)
我更改了sprogressasynctask类,而不是传递活动通过上下文-getApplicationContext(),但这使情况更糟。
任何人都可以帮我知道,为什么(firstrun)每次执行而不是仅执行一次。
btw,mine&amp;我的Firend设备都只有冰淇淋OS。
上午在这个阶段被束缚在5-6天之内,任何帮助都是非常感谢的。请帮助我摆脱此错误。
我假设您将firstRun
对象设置为onCreate
中的true
,否则将永远不会启动。
如果执行此操作,则应再次检查活动寿命,活动以on创建(将firstrun设置为true)开始。
使用此设置,该应用将在每个启动
ASyncTask
这个例外,我也可能会遇到相同的情况。异步在后台起作用,并在并行执行UI线程中发生异常。ASYNCTASK仍在运行尝试显示对话框的,该对话给出了此例外。在此异常堆栈跟踪之前,确切的原因也被打印在logcat中。检查您的logcat。
这有多么出乎意料!
我刚刚在静态类中尝试了变量
public static boolean FIRST_START = true;
和在我的活动类中,用常数替换了firstrun varialbe.firstrongtart,并设置contacts.firstrongtart = false of onstart()。现在,它仅一次调用该方法一次,即实际启动应用程序。然后,当我回到活动时再次被调用。
相同的方法,只使用了不同类别的不同var。这有多奇怪?如果有的话,这可能会对某人有所帮助。