ACRA没有捕获异常(使用parse.com自定义ReportSender)

时间:2016-04-28 15:20:59

标签: java android acra

我添加了最新的ACRA:

compile 'ch.acra:acra:4.8.5'

根据文档配置我的App类:

@ReportsCrashes(
        reportSenderFactoryClasses = {com.github.dht.screenger.crash.ParseSenderfactory.class}
)

...

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);

    // The following line triggers the initialization of ACRA
    ACRA.init(this);

}

定义发件人工厂:

public class ParseSenderfactory implements ReportSenderFactory {

// NB requires a no arg constructor.

public ReportSender create(Context context, ACRAConfiguration config){
    return new ParseSender();
    }
}

发件人:

public class ParseSender implements ReportSender {

    @Override
    public void send(Context context, CrashReportData report) throws ReportSenderException {

        // Get the ParseObject to be send to Parse
        CrashReportParseObject crashReport = getParseObject(report);

        // Associate the current logged user with the crash report
        ParseUser currentUser = ParseUser.getCurrentUser();
        crashReport.setUser(currentUser);

        // Send crash report to Parse
        crashReport.saveInBackground();
    }

    /**
     * Get a custom ParseObject for data collected by ACRA.
     * @param report
     * Report data collected by ACRA.
     * @return
     * A custom ParseObject containing relevant data.
     */
    private CrashReportParseObject getParseObject(Map<ReportField, String> report) {
        ReportField[] fields = ACRA.getConfig().customReportContent();
        if (fields.length == 0) {
            fields = ACRAConstants.DEFAULT_REPORT_FIELDS;
        }
        CrashReportParseObject finalReport = new CrashReportParseObject();
        for (ReportField field : fields) {
            finalReport.put(field.toString(), report.get(field));
        }
        return finalReport;
    }

}

和ParseObject:

@ParseClassName("CrashReport")
public class CrashReportParseObject extends ParseObject {

    public String getAndroidVersion(){
        return this.getString("ANDROID_VERSION");
    }

    public String getAppVersionName(){
        return this.getString("APP_VERSION_NAME");
    }

    public String getPhoneModel(){
        return this.getString("PHONE_MODEL");
    }

    public String getProduct(){
        return this.getString("PRODUCT");
    }

    public void setUser(ParseUser user){
        this.put("user", user);
    }
}

然而,ACRA没有发现我从活动中发出的异常:

@Override
public void onStart() {
    super.onStart();

    throw new RuntimeException("This is a crash");

}

我忘记了什么吗?

更新

取得了一些进展。看到没有创建CrashReportParseObject,因为Parse希望语法为ParseObject.create(&#34; CrashReport&#34;);

将ParseSender更改为:

public class ParseSender implements ReportSender {

    @Override
    public void send(Context context, CrashReportData report) throws ReportSenderException {

        Log.d("ACRA", "Sending log");
        // Get the ParseObject to be send to Parse
        ParseObject crashReport = getParseObject(report);

        // Associate the current logged user with the crash report
        ParseUser currentUser = ParseUser.getCurrentUser();
        crashReport.put("user", currentUser);

        Log.d("ACRA", crashReport.toString());
        // Send crash report to Parse
        crashReport.saveInBackground();
    }

    /**
     * Get a custom ParseObject for data collected by ACRA.
     *
     * @param report Report data collected by ACRA.
     * @return A custom ParseObject containing relevant data.
     */
    private ParseObject getParseObject(Map<ReportField, String> report) {
        ReportField[] fields = ACRA.getConfig().customReportContent();
        if (fields.length == 0) {
            fields = ACRAConstants.DEFAULT_REPORT_FIELDS;
        }
        ParseObject finalReport = new ParseObject("CrashReport");
        for (ReportField field : fields) {
            if (field.toString() != null && report.get(field) != null) {
                finalReport.put(field.toString(), report.get(field));
            }

        }
        return finalReport;
    }

}

并将CrashReportParseObject一起删除。现在我得到了#34; ACRA为com.github.dht.screenger&#34;捕获了一个RuntimeException。但仍然没有在parse.com上创建对象。这是时机吗?在获得保存对象的时间之前,应用程序是否会崩溃?

0 个答案:

没有答案