错误如下:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jctek.earthquake/com.jctek.earthquake.FragmentPreferences}: java.lang.RuntimeException: Subclasses of PreferenceActivity must override isValidFragment(String) to verify that the Fragment class is valid! com.jctek.earthquake.FragmentPreferences has not checked if fragment com.jctek.earthquake.UserPreferenceFragment is valid.
原因:SDK19以上的安全限制,需要重写isValidFragment
protected boolean isValidFragment (String fragmentName)
Added in API level 19
Subclasses should override this method and verify that the given fragment is a
valid type to be attached to this activity. The default implementation returns
true for apps built for android:targetSdkVersion older than KITKAT. For later
versions, it will throw an exception.
解决方法:
@Override
protected boolean isValidFragment (String fragmentName) {
return [YOUR_FRAGMENT_NAME].class.getName().equals(fragmentName);
}
或者
@Override
protected boolean isValidFragment (String fragmentName) {
return true;
}
解决Android应用中isValidFragment()方法的问题

本文解决了一个在Android应用中遇到的错误:无法启动活动,原因是子类PreferenceActivity未重写isValidFragment()方法来验证片段类的有效性。通过提供解决方法,即重写该方法并检查片段类名,可以解决此问题。
14

被折叠的 条评论
为什么被折叠?



