在下面的崩溃报告中,我看到了一些奇怪的东西。这发生在运行4.3的HTC One上,我无法访问这些手机进行调试,但看起来它在super.onCreate()第236行期间在我的PenelopeMainActivity(扩展Activity)期间创建了我的SettingFragment(扩展了PreferenceFragment)。 / p>
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.harmonicprocesses.penelopefree/com.harmonicprocesses.penelopefree.PenelopeMainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2517)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
at android.app.ActivityThread.access$600(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5789)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1129)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2354)
at android.content.res.Resources.getXml(Resources.java:994)
at android.preference.GenericInflater.inflate(GenericInflater.java:261)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
at com.harmonicprocesses.penelopefree.settings.SettingsFragment.onCreate(SettingsFragment.java:62)
at android.app.Fragment.performCreate(Fragment.java:1775)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1101)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
at android.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1989)
at android.app.Activity.onCreate(Activity.java:929)
at com.harmonicprocesses.penelopefree.PenelopeMainActivity.onCreate(PenelopeMainActivity.java:236)
at android.app.Activity.performCreate(Activity.java:5195)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2473)
... 11 more
现在,SettingsFragment使用在调用super.OnCreate()之后设置的XmlId。事实上,当调用OnCreate()时,不应该有一个SettingsFragment实例。我错过了什么吗?当进程被杀死时,这可能与onStop()和onStart()vs onCreate()有关吗?
但它应该从头开始重新创建onCreate(),即使设置菜单打开,也不要尝试构建设置菜单?正确?
这是onCreate方法和xmlId set方法 com.harmonicprocesses.penelopefree.settings.SettingsFragment.onCreate()
public SettingsFragment setXmlId(int xmlId){
mXmlId = xmlId;
return this;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAct = getActivity();
mRes = mAct.getResources();
mFrag = getFragmentManager();
// Load the preferences from an XML resource
addPreferencesFromResource(mXmlId); // <<<-------------------*** LINE 62 ***
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(preferenceChangeListener);
}
这是我创建SettingsFragment实例的地方:
public boolean onOptionsItemSelected(MenuItem item){
mHideHandler.removeCallbacks(mHideRunnable);
if (item.getItemId() == R.id.options_menu_special_efects) {
//Intent intent = new Intent(this, SpecialEffects.class);
mSettingsFragment = new SettingsFragment().setXmlId(R.xml.special_effects);
mSettingsFragment.setContext(mContext)
.setPurchaseManager(purchaseManager)
.setAudioProcessor(mAudioProcessor)
.setPcam(mPcamera);
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, mSettingsFragment)
.commit();
mFragmentViewGroup.setVisibility(View.VISIBLE);
mSystemUiHider.hide();
mSystemUiHider.disable();
}
return false;
}
答案 0 :(得分:2)
从它的外观来看,mXmlId
基于Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
为0。从你的onCreate
看,这并不奇怪。您在调用mXmlId
时onCreate
上的依赖。如果您的片段被屏幕旋转更改破坏,您将获得onDestroy&gt; onCreate使用Bundle
中的任何已保存值调用您的片段。您没有使用捆绑包,因此您不会重新填充该字段。
对于记录,“正确”的方法是使用setArguments(Bundle)
将构造函数args传递给Fragment
。有关创建片段的newInstance
模式的信息,请参阅http://developer.android.com/reference/android/app/Fragment.html。