我开始使用ACRA(https://github.com/ACRA/acra(进行崩溃报告。在测试时,一切都很完美。尽管如此,当我发布该应用程序时,我在Google Play管理中心中看到了一个错误,该错误是我发布的版本所犯的,由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 的讨论,我阅读了马尔科姆·库克提出的解决方案:
为了其他人的利益,我发现了我的问题是什么。
类 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);
放在哪里?我有它public void onCreate()
但它把这个java.lang.IllegalStateException
扔给了我。所以我想我应该试着把它放在protected void attachBaseContext(Context base)
,正如马尔科姆库克建议的那样。谁能确认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()
.它在我测试时对我有用。尽管如此,当我发布该应用程序时,我已经在Google Play控制台中看到崩溃,这是由ACRA.init(this);
引起的,正如我在问题中所解释的那样。所以我想我可以尝试把ACRA.init(this);
放在protected void attachBaseContext(Context base)
,正如马尔科姆·库克所建议的那样。谁能向我澄清这一点?
更新 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...
初始化现在已成功。