java - 我应该把 ACRA.init(this); 放在哪里?

标签 java android logging acra

我开始使用 ACRA ( https://github.com/ACRA/acra ) 进行崩溃报告。测试时,一切都很完美。尽管如此,当我发布该应用程序时,我在 Google Play Console 中看到了一个错误,该错误是我发布的版本中的新错误,由 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 上的讨论我读了马尔科姆·库克提出的解决方案:

For the benefit of anyone else I discovered what my problem was.

The class MyDBOpenHelper was being triggered from the onCreate method of a ContentProvider, which gets called before the application class's onCreate method. Resolved it for now by moving the acra init method call within the application class as follows

@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:

Practical Android: 14 Complete Projects on Advanced Techniques and Approaches ( https://www.amazon.com/dp/B078SK4W1M/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1 ) 的第 3 章提供了使用 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 Console 中看到了由 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...

现在初始化成功了。

关于java - 我应该把 ACRA.init(this); 放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57518920/

相关文章:

android - 日志标签最多可包含 23 个字符

java - 如何检查Graph是否连接

java - 如何完成三个Queue的实现以及三个Stack的实现?

java - 处理中的类中是否可以有另一个循环?

android - 如何将 onclick 处理程序传递给自定义对话框类

java - 将 slf4j 与 log4j2 结合使用

java - Java中的继承和多态

java - Android 蓝牙 ConnectThread 示例错误?

android - 无法解析资源 @layout/date_picker_selected_date

GWT 记录到文件