本文介绍了当Android的isValidFragment()从preferenceActivity被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于某些应用上,我是工作,对于设备与API级别19我得到异常

 致:java.lang.RuntimeException的:的preferenceActivity子类必须覆盖isValidFragment(字符串),以验证该片段类是有效的!玉米......没有如果片段COM ... $检查。已验证。
 

然后,我发现这些应用程序的Andr​​oid框架保护布尔isValidFragment(字符串fragmentName)获取调用,其中有code

 如果(getApplicationInfo()targetSdkVersion> = android.os.Build.VERSION_ codeS.KITKAT){
        抛出新的RuntimeException(
                的preferenceActivity子类必须覆盖isValidFragment(字符串)
                +来验证该片段类是有效!+ this.getClass()。的getName()
                + fragmentName +是有效的。+如果片段没有检查);
    } 其他 {
        返回true;
}
 

然后我试图复制错误

我把我的示例应用程序的code从 preferences活动示例并加入行<使用-SDK机器人:targetSdkVersion =19/> 在清单

但奇怪的是,我没有收到错误(isValidFragment()不获取调用在这种情况下)。

所以,请告诉我如何复制的错误在我的示例应用程序。

解决方案

好像是一个错误或4.4安全限制。 Workaraound是使用低于19任何仍与 preferenceActivity 兼容,然后硬着头皮用于与旧的目标进行编译。

我是用头模式为preferenceActivity(覆盖公共无效onBuildHeaders(名单<头>目标)),我假设OP是也极有可能是在地方,事情发生和崩溃。

在我的情况,我已经缩小这一例外<使用-SDK机器人:targetSdkVersion =19/> ,并在什么[14-18]构建目标将编译及运行没有问题。

建议(用于Eclipse):我从来没有搞砸直接搞砸这样的东西,但我如果您编译 preferenceActivity (也许片段)就任不同的项目,针对18岁或以下(双关语不打算:O),然后使用该项目作为作为你的目标奇巧(19)主体工程,也许就能避免死机的情况在运行 - 时间,同时还使用从最新版本需要的功能(只要这些功能在构建-18结合 preferenceActivity )。如果这没有成功,请尝试使用该项目中的的形式(pre-编译)而不是使用项目作为库。

更新:还注意到卡米尔塞维尼的回答。如果问题有什么关系,其他问题(50%的机会恕我直言),针对18 API所有应用程序都容易受到片段注入式攻击(看他的链接的问题)。

For some application on which I was working, for devices with API level 19 I'm getting exception

Caused by: java.lang.RuntimeException: Subclasses of PreferenceActivity must override isValidFragment(String) to verify that the Fragment class is valid! com... has not checked if fragment com...$. is valid.

Then, I found out that for those applications android frameworks protected boolean isValidFragment(String fragmentName) is getting called, which has code

if (getApplicationInfo().targetSdkVersion  >= android.os.Build.VERSION_CODES.KITKAT) {
        throw new RuntimeException(
                "Subclasses of PreferenceActivity must override isValidFragment(String)"
                + " to verify that the Fragment class is valid! " + this.getClass().getName()
                + " has not checked if fragment " + fragmentName + " is valid.");
    } else {
        return true;
}

Then I tried to replicate the error

I took my sample app's code from Preferences Activity Exampleand added line <uses-sdk android:targetSdkVersion="19" /> in manifest.

But strangely, I'm not getting the error(isValidFragment() not getting called in that case).

So please tell me how to replicate that error in my sample app.

解决方案

Seems to be a bug or a 4.4 security restriction. Workaraound is to use anything below 19 that is still compatible with PreferenceActivity, and bite the bullet for compiling with an older target.

I am using the headers "pattern" for the PreferenceActivity (overriding public void onBuildHeaders(List<Header> target)), and I assume the OP is too, most likely being the place where stuff happens and crashes.

In my case, I have narrowed this exception to <uses-sdk android:targetSdkVersion="19" />, and anything in [14-18] build targets will compile and run without issues.

Suggestion (for Eclipse): I never messed directly messed with such stuff, but I'm assuming if you compile your PreferenceActivity (and maybe fragments) on a different project, targeting 18 or under (pun not intended :O ), and then using that project as a library for your main project targeting KitKat (19), perhaps you can avoid the crash scenario at run-time while still using the features you need from the latest build (as long as those features aren't in the build-18-bound PreferenceActivity). If this does not succeed, try with that project in jar form (pre-compiled) instead of using project as library.

UPDATE: also take note of Camille Sévigny's answer. If the issue has anything to do with that other question (50% chance IMHO), all apps targeting API 18 are vulnerable to fragment injection attacks (see his linked question).

这篇关于当Android的isValidFragment()从preferenceActivity被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 04:01