[英]Where should I place ACRA.init(this);?
我開始使用ACRA( https://github.com/ACRA/acra )進行崩潰報告。 在測試時,一切都很完美。 但是,當我發布該應用程序時,我在Google Play控制台中看到了由ACRA.init(this);
引起的我發布的版本中的新錯誤ACRA.init(this);
:
java.lang.RuntimeException:
at android.app.ActivityThread.handleBindApplication
(ActivityThread.java:6209)
at android.app.ActivityThread.access$1200 (ActivityThread.java:236)
at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1784)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:214)
at android.app.ActivityThread.main (ActivityThread.java:7032)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run
(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:965)
Caused by: java.lang.IllegalStateException:
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1666)
at android.app.ContextImpl.startService (ContextImpl.java:1611)
at android.content.ContextWrapper.startService
(ContextWrapper.java:677)
at org.acra.sender.SenderServiceStarter.startService
(SenderServiceStarter.java:43)
at org.acra.util.ApplicationStartupProcessor.sendApprovedReports
(ApplicationStartupProcessor.java:75)
at org.acra.ACRA.init (ACRA.java:230)
at org.acra.ACRA.init (ACRA.java:156)
at org.acra.ACRA.init (ACRA.java:139)
at com.myapplication.MyApplication.onCreate
(MyApplication.java:132)
at android.app.Instrumentation.callApplicationOnCreate
(Instrumentation.java:1154)
at android.app.ActivityThread.handleBindApplication
(ActivityThread.java:6204)
MyApplication.java:132
的內容是:
ACRA.init(this);
具有諷刺意味的是,這意味着初始化ACRA會導致崩潰。 為了提供一些上下文,這是我擁有ACRA.init(this)
:
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
ACRA.init(this);
我在https://groups.google.com/forum/#!topic/acra-discuss/XUKJ5dFHBl0上閱讀討論,並且閱讀了Malcolm Cooke提出的解決方案:
為了別人的利益,我發現了我的問題所在。
MyDBOpenHelper類是從ContentProvider的onCreate方法觸發的,該方法在應用程序類的onCreate方法之前被調用。 現在通過在應用程序類內移動acra init方法調用來解決此問題,如下所示
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
ACRA.init(this);
// some of your own operations before content provider will launch
}
我應該把ACRA.init(this);
放在哪里ACRA.init(this);
? 我在public void onCreate()
擁有它,但它向我拋出了此java.lang.IllegalStateException
。 因此,我想我應該嘗試將其放到Malcolm Cooke建議的protected void attachBaseContext(Context base)
。 任何人都可以確認ACRA.init(this);
的正確位置ACRA.init(this);
是嗎 謝謝。
更新1:
實用Android的第3章:14個高級技術和方法的完整項目( https://www.amazon.com/dp/B078SK4W1M/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1 )提供了使用以下項目的示例ACRA和他們的MyApplication.java文件中使用以下命令:
package com.wickham.android.crashlog;
import org.acra.annotation.ReportsCrashes;
import org.acra.*;
import android.app.Application;
@ReportsCrashes(
customReportContent = { ReportField.REPORT_ID,
ReportField.APP_VERSION_CODE,
ReportField.APP_VERSION_NAME,
ReportField.PACKAGE_NAME,
ReportField.PHONE_MODEL,
ReportField.ANDROID_VERSION,
ReportField.STACK_TRACE,
ReportField.TOTAL_MEM_SIZE,
ReportField.AVAILABLE_MEM_SIZE,
ReportField.DISPLAY,
ReportField.USER_APP_START_DATE,
ReportField.USER_CRASH_DATE,
ReportField.LOGCAT,
ReportField.DEVICE_ID,
ReportField.SHARED_PREFERENCES,
ReportField.CUSTOM_DATA },
//formKey = "",
formUri = "https://example.com/crashed.php",
httpMethod = org.acra.sender.HttpSender.Method.POST,
mode = ReportingInteractionMode.TOAST,
resToastText = R.string.msg_crash_text)
public class MyApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();
ACRA.init(this);
}
}
他們正在放置ACRA.init(this);
在public void onCreate()
。 我測試時對我有用。 但是,當我發布該應用程序時,我已經看到由ACRA.init(this);
引起的Google Play控制台崩潰ACRA.init(this);
正如我在問題中解釋的那樣。 所以我想我可以嘗試放置ACRA.init(this);
在protected void attachBaseContext(Context base)
,如Malcolm Cooke建議的那樣。 誰能向我澄清一下?
更新2:
閱讀https://github.com/ACRA/acra/wiki/BasicSetup ,我看到他們有這個:
import org.acra.*;
import org.acra.annotation.*;
@AcraCore(buildConfigClass = BuildConfig.class)
public class MyApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// The following line triggers the initialization of ACRA
ACRA.init(this);
}
}
更新3:
即使將其放在attachBaseContext
,ACRA也會使我的應用崩潰:
java.lang.RuntimeException:
at android.app.LoadedApk.makeApplication (LoadedApk.java:1164)
at android.app.ActivityThread.handleBindApplication (ActivityThread.java:6529)
at android.app.ActivityThread.access$1900 (ActivityThread.java:267)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1963)
at android.os.Handler.dispatchMessage (Handler.java:109)
at android.os.Looper.loop (Looper.java:207)
at android.app.ActivityThread.main (ActivityThread.java:7470)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:958)
Caused by: java.lang.IllegalStateException:
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1842)
at android.app.ContextImpl.startService (ContextImpl.java:1797)
at android.content.ContextWrapper.startService (ContextWrapper.java:664)
at org.acra.sender.SenderServiceStarter.startService (SenderServiceStarter.java:43)
at org.acra.util.ApplicationStartupProcessor.sendApprovedReports (ApplicationStartupProcessor.java:75)
at org.acra.ACRA.init (ACRA.java:230)
at org.acra.ACRA.init (ACRA.java:156)
at org.acra.ACRA.init (ACRA.java:139)
at com.myapp.MyApplication.attachBaseContext (MyApplication.java:126)
at android.app.Application.attach (Application.java:224)
at android.app.Instrumentation.newApplication (Instrumentation.java:1128)
at android.app.LoadedApk.makeApplication (LoadedApk.java:1156)
根據https://github.com/ACRA/acra/issues/630上的信息,我的解決方案使用的是:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
ACRA.init(this, new ConfigurationBuilder(this).build(), false);
MultiDex.install(this);
}
在日志中,我可以看到LOGCAT
顯示以下行:
08-18 16:31:50.489 I/ACRA (11890): ACRA is enabled for com.myapp, initializing...
現在初始化成功。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.