java.lang.RuntimeException:无法启动活动android.content.res.Resources $ NotFoundException:资源ID#0x7f110002

时间:2019-01-15 05:49:38

标签: java android android-layout android-fragments

尝试打开首选项活动时,有时会出现广告错误,并且应用程序崩溃。问题是我不知道为什么有时会发生这种情况,有时却不会发生。 从我的角度来看,代码本身似乎是正确的。例如,当我在System.out.println(android.R.id.content);之后添加setupActionBar(); 我通常会在模拟的Nexus 9(Android 6.0,API 23)上收到此错误。

public class SettingsActivity extends AppCompatPreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupActionBar();

        // if I add ex. System.out.println(android.R.id.content); it works.

        // load settings fragment
        getFragmentManager().beginTransaction().replace(android.R.id.content,
                new SettingsPreferenceFragment()).commit();
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    private void setupActionBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            // Show the Up button in the action bar.
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    /**
     * This method stops fragment injection in malicious applications.
     * Make sure to deny any unknown fragments here.
     */
    protected boolean isValidFragment(String fragmentName) {
        return PreferenceFragment.class.getName().equals(fragmentName)
                || SettingsPreferenceFragment.class.getName().equals(fragmentName);
    }

    /**
     * This fragment shows general preferences only. It is used when the
     * activity is showing a two-pane settings UI.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class SettingsPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_settings);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == android.R.id.home) {
                startActivity(new Intent(getActivity(), SettingsActivity.class));
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        finish();

        super.onBackPressed();
    }

}
public abstract class AppCompatPreferenceActivity extends PreferenceActivity {

    private AppCompatDelegate mDelegate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getDelegate().installViewFactory();
        getDelegate().onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        getDelegate().onPostCreate(savedInstanceState);
    }

    public ActionBar getSupportActionBar() {
        return getDelegate().getSupportActionBar();
    }

    public void setSupportActionBar(@Nullable Toolbar toolbar) {
        getDelegate().setSupportActionBar(toolbar);
    }

    @Override
    public MenuInflater getMenuInflater() {
        return getDelegate().getMenuInflater();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        getDelegate().setContentView(layoutResID);
    }

    @Override
    public void setContentView(View view) {
        getDelegate().setContentView(view);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        getDelegate().setContentView(view, params);
    }

    @Override
    public void addContentView(View view, ViewGroup.LayoutParams params) {
        getDelegate().addContentView(view, params);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        getDelegate().onPostResume();
    }

    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        super.onTitleChanged(title, color);
        getDelegate().setTitle(title);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        getDelegate().onConfigurationChanged(newConfig);
    }

    @Override
    protected void onStop() {
        super.onStop();
        getDelegate().onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getDelegate().onDestroy();
    }

    public void invalidateOptionsMenu() {
        getDelegate().invalidateOptionsMenu();
    }

    private AppCompatDelegate getDelegate() {
        if (mDelegate == null) {
            mDelegate = AppCompatDelegate.create(this, null);
        }
        return mDelegate;
    }
}

打开此活动时,出现的错误如下:

W/ResourceType: For resource 0x7f110002, entry index(2) is beyond type entryCount(1)
W/ResourceType: Failure getting entry for 0x7f110002 (t=16 e=2) (error -75)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: de.*****.app, PID: 14679
    java.lang.RuntimeException: Unable to start activity ComponentInfo{de.*****.app/de.*****.app.SettingsActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f110002
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f110002
        at android.content.res.Resources.getValue(Resources.java:1351)
        at android.content.res.Resources.loadXmlResourceParser(Resources.java:2774)
        at android.content.res.Resources.getXml(Resources.java:1214)
        at android.preference.PreferenceActivity.loadHeadersFromResource(PreferenceActivity.java:810)
        at de.*****.app.SettingsActivity.onBuildHeaders(SettingsActivity.kt:56)
        at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:596)
        at de.*****.app.AppCompatPreferenceActivity.onCreate(AppCompatPreferenceActivity.kt:23)
        at de.*****.app.SettingsActivity.onCreate(SettingsActivity.kt:33)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Application terminated.

希望有人知道,可能是什么问题。

3 个答案:

答案 0 :(得分:0)

您是否将此res / xml / preference_headers.xml添加到资源目录中?

答案 1 :(得分:0)

  

您做得很好,但问题出在这里。

 @Override
public void onBackPressed() {
    finish();
    super.onBackPressed();
} 

您要在执行super方法之前调用finish,您所要做的就是用此替换。

 public void onBackPressed() {
    super.onBackPressed();
    finish();
} 

uninstall the application and then reinstall.

答案 2 :(得分:0)

谢谢。我实际上发现了问题。当非常仔细地阅读错误消息时,我注意到它在Kotlin文件上引用了-但是我使用Java。 由于清除和重建不起作用,因此我复制了Java代码,将文件转换为Kotlin,删除了Kotlin文件并重新创建了Java文件。现在一切正常。