当前位置: 首页 > 知识库问答 >
问题:

致命异常:main(Android.content.res.resources$NotFoundException:Resource ID#0x0)

蒲昊苍
2023-03-14

我正试图使用Android Studio 3.4.1运行一个应用程序,但当我试图调试或运行它时,应用程序崩溃了。

我已经遇到过一次无法启动activity ComponentInfo错误,但是在一个使用Kotlin的项目上(该项目使用Java),并且有一个不同的异常。我尝试编辑清单、修改活动并将mipmap文件夹中的所有图像更改为drawable文件夹,但这些都不起作用。

下面是我的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.testapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="AllowBackup,GoogleAppIndexingWarning">
        <activity
            android:name=".Main2Activity"
            android:label="@string/title_activity_main2" />
        <activity
            android:name=".NotificationsActivity"
            android:label="@string/title_notifications" />
        <activity
            android:name=".DashboardActivity"
            android:label="@string/title_dashboard" />

        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.testapp.DashboardActivity" />

        <activity
            android:name=".BaseActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
package com.example.testapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class BaseActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    protected BottomNavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId()); //References this line in the logs

        navigationView = findViewById(R.id.nav_view);
        navigationView.setOnNavigationItemSelectedListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        updateNavigationBarState();
    }

    // Remove inter-activity transition to avoid screen tossing on tapping bottom navigation items
    @Override
    public void onPause() {
        super.onPause();
        overridePendingTransition(0, 0);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
        navigationView.postDelayed(new Runnable() {
            @Override
            public void run() {
                int itemId = item.getItemId();
                if (itemId == R.id.navigation_home) {
                    BaseActivity.this.startActivity(new Intent(BaseActivity.this, BaseActivity.class));
                } else if (itemId == R.id.navigation_dashboard) {
                    BaseActivity.this.startActivity(new Intent(BaseActivity.this, DashboardActivity.class));
                } else if (itemId == R.id.navigation_notifications) {
                    BaseActivity.this.startActivity(new Intent(BaseActivity.this, NotificationsActivity.class));
                }
                BaseActivity.this.finish();
            }
        }, 300);
        return true;
    }

    private void updateNavigationBarState(){
        int actionId = getNavigationMenuItemId();
        selectBottomNavigationBarItem(actionId);
    }

    void selectBottomNavigationBarItem(int itemId) {
        Menu menu = navigationView.getMenu();
        for (int i = 0, size = menu.size(); i < size; i++) {
            MenuItem item = menu.getItem(i);
            boolean shouldBeChecked = item.getItemId() == itemId;
            if (shouldBeChecked) {
                item.setChecked(true);
                break;
            }
        }
    }

    int getContentViewId() {
        return 0;
    }

    int getNavigationMenuItemId() {
        return 0;
    }

}
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.testapp, PID: 10261
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testapp/com.example.testapp.BaseActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2827)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2902)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1603)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:169)
        at android.app.ActivityThread.main(ActivityThread.java:6578)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
        at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:204)
        at android.content.res.Resources.loadXmlResourceParser(Resources.java:2133)
        at android.content.res.Resources.getLayout(Resources.java:1142)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.testapp.BaseActivity.onCreate(BaseActivity.java:18)
        at android.app.Activity.performCreate(Activity.java:7016)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2780)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2902) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1603) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:169) 
        at android.app.ActivityThread.main(ActivityThread.java:6578) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

共有1个答案

丁德义
2023-03-14

尝试改变

setContentView(getContentViewId());

Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
 类似资料: