java.lang.RuntimeException : Unable to start activity android. content.res.Resources$NotFoundException: 资源 ID #0x7f110002

标签 java android android-layout android-fragments

尝试打开偏好设置 Activity 时,我有时会收到广告错误并且应用程序崩溃。问题是我不知道为什么有时会发生这种情况,有时却不会。 从我的角度来看,代码本身似乎是正确的。例如,当我在 setupActionBar(); 之后添加一个 System.out.println(android.R.id.content); 我通常会在模拟的 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;
    }
}

打开此 Activity 时,我得到的错误如下所示:

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.

希望有人知道问题可能出在哪里。

最佳答案

这个res/xml/preference_headers.xml是你在资源目录下添加的吗?

关于java.lang.RuntimeException : Unable to start activity android. content.res.Resources$NotFoundException: 资源 ID #0x7f110002,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54193347/

相关文章:

java - 如何使用 datastax java 驱动程序有效地使用批量写入 cassandra?

java - 如何在 JFrame 中的两个标签之间绘制箭头/线?

Android SQLite 数据库损坏

Android Studio 布局 : Trying to make button 50% width and 50% height,,中间有按钮

java - 动态添加 View 到LinearLayout

java - 不同语言、不同字符的网页的URL结构

java - Netty - 客户端/服务器聊天

java - 运行 Activity 时无限循环

android - 在 AmfPHP 中开发的 Web 服务——在 IOS 和 Android 中使用?

android - 自定义通知布局在 Android 6.0 和 4.3 上崩溃