继承PreferenceActivity,报需要验证fragment是否有效

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误如下:

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;
}





Failed to execute the transaction: tId:-1820442509 ClientTransaction{ tId:-1820442509 transactionItems=[ tId:-1820442509 LaunchActivityItem{activityToken=android.os.BinderProxy@291a0d4,intent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.myapplication01/.MainActivity },ident=160997161,info=ActivityInfo{3865407 com.example.myapplication01.MainActivity},curConfig={1.0 310mcc260mnc [en_US] ldltr sw411dp w411dp h914dp 420dpi nrml long port finger qwerty/v/v dpad/v winConfig={ mBounds=Rect(0, 0 - 1080, 2400) mAppBounds=Rect(0, 0 - 1080, 2400) mMaxBounds=Rect(0, 0 - 1080, 2400) mDisplayRotation=ROTATION_0 mWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=ROTATION_0} as.4 s.111 fontWeightAdjustment=0},overrideConfig={1.0 310mcc260mnc [en_US] ldltr sw411dp w411dp h914dp 420dpi nrml long port finger qwerty/v/v dpad/v winConfig={ mBounds=Rect(0, 0 - 1080, 2400) mAppBounds=Rect(0, 0 - 1080, 2400) mMaxBounds=Rect(0, 0 - 1080, 2400) mDisplayRotation=ROTATION_0 mWindowingMode=fullscreen mActivityType=standard mAlwaysOnTop=undefined mRotation=ROTATION_0} as.4 s.2 fontWeightAdjustment=0},deviceId=0,referrer=com.android.shell,procState=2,state=null,persistentState=null,pendingResults=null,pendingNewIntents=null,sceneTransitionInfo=null,profilerInfo=null,assistToken=android.os.BinderProxy@c3dbc88,shareableActivityToken=android.os.BinderProxy@eeae621,activityWindowInfo=ActivityWindowInfo{isEmbedded=false, taskBounds=Rect(0, 0 - 1080, 2400), taskFragmentBounds=Rect(0, 0 - 1080, 2400)}} tId:-1820442509 ResumeActivityItem{mActivityToken=android.os.BinderProxy@291a0d4,procState=-1,isForward=true,shouldSendCompatFakeFocus=false} tId:-1820442509 Target activity: com.example.myapplication01.MainActivity tId:-1820442509 ] tId:-1820442509 }
最新发布
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值