Android 无法启动 Activity - java.lang.RuntimeException: 无法启动 Activity ComponentInfo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29090794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:20:26  来源:igfitidea点击:

Android cannot start Activity - java.lang.RuntimeException: Unable to start activity ComponentInfo

javaandroidandroid-studio

提问by Kenny Worden

Here's a problem that's really getting to me, whenever I try to launch my application's main activity, I get the error in LogCat:

这是一个真正困扰我的问题,每当我尝试启动应用程序的主要活动时,我都会在 LogCat 中收到错误消息:

java.lang.RuntimeException: Unable to start activity ComponentInfo{me.kworden.atic/me.kworden.atic.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f0c00a9

java.lang.RuntimeException:无法启动活动 ComponentInfo{me.kworden.atic/me.kworden.atic.MainActivity}:android.content.res.Resources$NotFoundException:资源 ID #0x7f0c00a9

The stack trace says that the problem is my super.onCreate(savedInstanceState)in my onCreate()method, which is frustrating me, because I can't control the superclass method.

堆栈跟踪说问题出super.onCreate(savedInstanceState)在我的onCreate()方法中,这让我很沮丧,因为我无法控制超类方法。

The content layout I'm using is only a LinearLayout, so I don't think it is necessary for you all to see my xml file, although I will post on request.

我正在使用的内容布局只是一个LinearLayout.

I've seen the other similar problems on SO, but the only answers were "Clean/Rebuild the project" which hasn't done anything for me. I'm using Android Studio ver. 1.1.0 if this is a known problem, but I haven't seen any known bugs.

我在 SO 上看到了其他类似的问题,但唯一的答案是“清理/重建项目”,这对我没有任何帮助。我正在使用 Android Studio 版本。1.1.0 如果这是一个已知问题,但我还没有看到任何已知的错误。

Here is my MainActivity, I've stripped out most of the code in order to pinpoint the problem, but now it seems that super.onCreateis the culprit; despite being extremely barebones, my app still fails to even start (i.e. I get "Unfortunately, app has stopped.").

这是我的 MainActivity,为了查明问题,我删除了大部分代码,但现在看来这super.onCreate是罪魁祸首;尽管非常准系统,我的应用程序仍然无法启动(即我收到“不幸的是,应用程序已停止。”)。

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // Create the activity and set the main content view //
        super.onCreate(savedInstanceState); // This is line 17, per the stack trace //
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the main menu to the toolbar //
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();

        if(id == R.id.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Here is my full stack trace:

这是我的完整堆栈跟踪:

03-16 23:16:33.572    1494-1494/me.kworden.atic E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: me.kworden.atic, PID: 1494
    java.lang.RuntimeException: Unable to start activity ComponentInfo{me.kworden.atic/me.kworden.atic.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f0c00a9
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f0c00a9
            at android.content.res.Resources.getValue(Resources.java:1233)
            at android.content.res.Resources.getDrawable(Resources.java:756)
            at android.content.Context.getDrawable(Context.java:402)
            at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3514)
            at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3561)
            at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1916)
            at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:151)
            at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
            at me.kworden.atic.MainActivity.onCreate(MainActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
????????????at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
????????????at android.app.ActivityThread.access0(ActivityThread.java:144)
????????????at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
????????????at android.os.Handler.dispatchMessage(Handler.java:102)
????????????at android.os.Looper.loop(Looper.java:135)
????????????at android.app.ActivityThread.main(ActivityThread.java:5221)
????????????at java.lang.reflect.Method.invoke(Native Method)
????????????at java.lang.reflect.Method.invoke(Method.java:372)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

采纳答案by Kenny Worden

If anyone stumbles on this in the future I want to make sure that you can get an answer...

如果将来有人偶然发现此问题,我想确保您能得到答案...

The problem was that certain tags that are available only to Material and Android 5.0+ were being used in my styles. So I instead made a res/values-v21/styles.xmlfolder and put my appropriate settings in there. That fixed the issue.

问题是我的样式中使用了某些仅适用于 Material 和 Android 5.0+ 的标签。所以我创建了一个res/values-v21/styles.xml文件夹并将我的适当设置放在那里。这解决了问题。

回答by Rakesh Kalashetti

Remove setContentView(R.layout.activity_main);method

移除setContentView(R.layout.activity_main);方法

after removing this method if it run successfully then your layout id cannot be gernerated.

删除此方法后,如果它运行成功,则无法生成您的布局 ID。

and also make sure that your layout id is generated or not in R.javafile

并确保您的布局 ID 是否在R.java文件中生成

回答by Muhammad Waleed

open manifest file check application package and activity. that is included or not included in file and also check style of application

打开清单文件检查应用程序包和活动。包含或不包含在文件中,并检查应用程序的风格

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="yourapplicationpackage" >
       <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        >
        <activity
            android:name=".MainActivity"
            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>