首页 > 代码库 > 继承PreferenceActivity 需要验证Frag是否有效

继承PreferenceActivity 需要验证Frag是否有效

错误如下:

Caused by: java.lang.RuntimeException: Subclasses of PreferenceActivity must override isValidFragment(String) to verify that the Fragment class is valid! com.example.silence.otherapp.PreferenceActivityTest has not checked if fragment com.example.silence.otherapp.PreferenceActivityTest$Prefs1Fragment 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;
}

继承PreferenceActivity 需要验证Frag是否有效