片段实例化崩溃

时间:2014-02-04 19:59:11

标签: java android android-fragments preferenceactivity

我的部分用户遇到了崩溃问题,这是Google Play开发者控制台崩溃报告中显示的错误:

Unable to start activity ComponentInfo{com.havens1515.autorespond/com.havens1515.autorespond.SettingsMenuNew}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.havens1515.autorespond.NotificationOptions: make sure class name exists, is public, and has an empty constructor that is public

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

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

这里有一些显示这些方法的代码:

public class NotificationOptions extends PreferenceFragment
{
    public NotificationOptions()
    {

    }

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

1 个答案:

答案 0 :(得分:5)

这可能是因为删除了你的碎片。

要重现,构建混淆的APK,在开发人员选项中启用“不要保持活动”,打开包含崩溃的片段的活动。最小化主页按钮并从最近恢复应用程序。

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

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

如果您正在使用Gradle构建系统

buildTypes {
    debug {
        runProguard false
    }

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

而proguard-project.txt至少应包含这些规则

-keep public class * extends android.preference.PreferenceFragment

如果你正在使用支持片段

-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中已包含一些规则,因此只根据您的需要添加缺失。

相关问题