【问题标题】:How to fix R.string errors when converting to library with ADT14+ android使用 ADT14+ android 转换为库时如何修复 R.string 错误
【发布时间】:2013-12-23 07:49:44
【问题描述】:

我正在尝试将我的应用程序项目转换为库,我收到类似的错误

resDialogTitle = R.string.crash_dialog_title,

The value for annotation attribute ReportsCrashes.resDialogTitle must be a constant expression  

它们在 ACRA 标头中使用

@ReportsCrashes(formKey = "--", mode = ReportingInteractionMode.DIALOG, mailTo = "--", customReportContent = {
        ReportField.APP_VERSION_CODE, ReportField.APP_VERSION_NAME,
        ReportField.ANDROID_VERSION, ReportField.PHONE_MODEL,
        ReportField.CUSTOM_DATA, ReportField.STACK_TRACE, ReportField.LOGCAT }
// resToastText = R.string.crash_toast_text, // optional, displayed as soon as
// the crash occurs, before collecting data which can take a few seconds
        resDialogText = getString(R.string.crash_dialog_text),
        resDialogIcon = android.R.drawable.ic_dialog_info, // optional.
// default
// is
// a
// warning
// sign
//resDialogTitle = R.string.crash_dialog_title, // optional. default is your
// application name
resDialogCommentPrompt = R.string.crash_dialog_comment_prompt, // optional. when
// defined, adds
// a user text
// field input
// with this
// text resource
// as a label
resDialogOkToast = R.string.crash_dialog_ok_toast // optional. displays a Toast
// message when the user
// accepts to send a report.
)
public class MainAppClass extends Application {

    private static MainAppClass singleton;

如何解决这个问题?

【问题讨论】:

    标签: android android-library acra


    【解决方案1】:

    在 ACRA,自 ADT 14 起,R fields are not constants in library project。 2个解决方案:

    1. 在使用库的应用中声明 R.string.crash_dialog_text
    2. 您可以dynamically 使用 ACRA.getConfig() 设置这些:

    方法 ACRA.getConfig() 返回一个 ACRAConfiguration 对象,该对象为每个 @ReportsCrashes 配置项提供一个设置器。

    例如:

        ACRA.getConfig().setResDialogText(R.string.crash_dialog_text);
    

    记得在你的 onCreate() 方法中调用 ACRA.init(this);

    您可以通过这种方式设置您需要的所有字段。检查here

    【讨论】:

    • 您需要 Context 才能使用 getString()。您可以使用 Activity 的上下文。
    • 你能试试吗,它对我不起作用,因为文本 ReportCrashes i 在类块 {} 上方
    • 它与应用程序类一起使用,发布在顶部相关部分。亲爱的 Melquiades 我不能放 getString 等,因为它不在课堂主体中
    • stackoverflow.com/questions/23078016/…。只需将注释值设置为 0,然后在调用 init 之前/之后设置这些动态配置。值得注意的是,ACRA 源显示下一个版本将发布一个采用动态配置的 init 方法:github.com/ACRA/acra/blob/master/src/main/java/org/acra/…
    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2023-02-19
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多