片段实例化崩溃 [英] Fragment Instantiation crash

查看:58
本文介绍了片段实例化崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一些用户正在崩溃,这是在Google Play开发者控制台上的崩溃报告中显示的错误:

Some of my users are experiencing crashes, and this is the error that shows in the crash report on the Google Play Developer Console:

无法启动活动ComponentInfo {com.havens1515.autorespond/com.havens1515.autorespond.SettingsMenuNew}:android.app.Fragment $ InstantiationException:无法实例化com.havens1515.autorespond.NotificationOptions片段:确保类名称存在,是公共的,并且有一个空的公共构造函数

用户说在上面的错误中提到打开 SettingsMenuNew 夹内部的任何设置菜单时都会发生这种情况,但我没有在手机上遇到崩溃的情况. SettingsMenuNew PreferenceActivity ,所有子菜单都是 PreferenceFragment

The users say that this occurs when opening any settings menu inside of SettingsMenuNew wich is mentioned in the error above, but I don't experience the crash on my phone. SettingsMenuNew is a PreferenceActivity and all of the submenus are PreferenceFragment

每个 PreferenceFragment 都有一个空的构造函数,我不知道问题可能是什么.在其他人的问题中,我也看到它需要 newInstance 方法,但是如果我不在片段中添加任何其他参数,我认为我并不需要.

Each PreferenceFragment has an empty constructor, and I don't know what else the issue could be. I also saw in someone else's question that it needs the newInstance method, but I don't think I really need that if I'm not putting any other arguments into the fragment.

以下是显示这些方法的代码:

here is some the code showing those methods:

public class NotificationOptions extends PreferenceFragment
{
    public NotificationOptions()
    {

    }

    public static NotificationOptions newInstance(int title, String message)
    {
        NotificationOptions f = new NotificationOptions();
        return f;
    }
    ...
}

推荐答案

这可能是由于Proguard删除了您的片段而引起的.

This probably happens due to proguard removing your Fragments.

要重现,请构建模糊的APK,在开发人员选项中启用不要保留活动",然后打开包含崩溃的Fragment的Activity.最小化主页按钮,并从最新消息中恢复应用.

To reproduce, build the obfuscated APK, enable "Don't Keep Activities" in developer options, open the Activity wich contains the Fragment that crashes. Minimize on home button and restore app from recents.

要将合并的Proguard配置与默认配置和您在ADT中的配置合并,您应该在project.properties中指定

To merge proguard configurations with default one and yours in ADT you should specify in project.properties

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

如果您使用的是Gradle构建系统

If you're using Gradle build system

buildTypes {
    debug {
        runProguard false
    }

    release {
        runProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
    }
}

并且proguard-project.txt应该至少包含这些规则

And proguard-project.txt should contain at least these rules

-keep public class * extends android.preference.PreferenceFragment

如果您正在使用支持片段

And if you're using support Fragments

-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.support.v4.app.FragmentActivity

别忘了$ {sdk.dir}/tools/proguard/proguard-android.txt中已经包含一些规则,因此仅根据您的需要添加缺少的规则.

Don't forget that there are already some rules contained in ${sdk.dir}/tools/proguard/proguard-android.txt so add only missing based on your needs.

这篇关于片段实例化崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆