Unable to instantiate activity ComponentInfo

2019-01-20 01:16发布

问题:

I am developing an application for Froyo as minimum version and Gingerbread as the target version. So, the manifest shows:

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10"/>

I have an emulator and a Nexus One with Gingerbread, and the application deploys and installs correctly. But when it starts, it crashes, and the error thrown in the log is:

09-18 06:35:11.240: ERROR/AndroidRuntime(382): FATAL EXCEPTION: main
09-18 06:35:11.240: ERROR/AndroidRuntime(382): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.my.package/com.my.package.Dashboard}: java.lang.ClassNotFoundException: com.my.package.Dashboard in loader dalvik.system.PathClassLoader[/data/app/com.my.package-2.apk]
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at android.os.Looper.loop(Looper.java:130)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at java.lang.reflect.Method.invokeNative(Native Method)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at java.lang.reflect.Method.invoke(Method.java:507)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at dalvik.system.NativeStart.main(Native Method)
09-18 06:35:11.240: ERROR/AndroidRuntime(382): Caused by: java.lang.ClassNotFoundException: com.my.package.Dashboard in loader dalvik.system.PathClassLoader[/data/app/com.my.package-2.apk]
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
09-18 06:35:11.240: ERROR/AndroidRuntime(382):     ... 11 more

And yes, the activity "Dashboard" is declared in the Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.my.package"
      android:versionCode="1"
      android:versionName="1.0"
      android:installLocation="auto">

      <activity android:name=".Dashboard"
              android:label="@string/app_name"
              android:screenOrientation="portrait">

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

      ...

So what can this problem be related to?? This is driving me nuts.

回答1:

I had this problem as well developing under Eclipse 4.2. My main activity was referenced by the AndroidManifest.xml file, included the correct package and name, but still failed to load. After a bit of debugging and commenting out things, I discovered that an interface that my main activity was implementing was the source of the problem. This interface is defined in a separate project, but I had failed to reference that project in the Java Build Path. So, when I built things, they would build correctly, but I'd immediately encounter a force close when I launched, with the same error as this question.

To correct this, I right-clicked on the Android project, selected Java Build Path, added my 'common code' project to the Projects tab and then checked the box for that same project on the Order and Export tab. After that, everything worked.



回答2:

I've fixed this problem easily by restoring the project properties:

  • Right-click on your project
  • Android tools
  • Fix project properties

Then your dependencies will be restored as new and everything will be ok!



回答3:

My solution was in Project --> Properties --> Java Build Path --> Order and Export, you should tick the entries you use. BUT also i experienced if you tick to Android.jar, it says "Conversion to Dalvik format failed with error 1"

Also removing all of your libraries except Android.x.x and then cleaning your project by Project --> Clean and then adding your libraries back (don't forget to tick in order and export) may solve the "Conversion to Dalvik format failed with error 1" thing.

And one more place you need to check is Properties --> Java Compiler --> JDK Compliance.



回答4:

For me, as it was during an upgrade of phonegap (from 1.4 to 1.8.1 ). I had to update the .project file on root project. I create a new project and compare the new file with the former.



回答5:

You can fix this error by correcting the class name of your activity in your manifest file



回答6:

I solved my issue by checking the Android Private Libraries and Android dependencies under the Order and Export in the Java Build path



回答7:

For the record, another simple reason that can cause this, that may have been overlooked when starting a project, is that the activity you wish to launch may not have extended Activity or be an empty class etc..



回答8:

As it turns out, the project files were missing some of the builders required for the apk to be correctly made. That's why it was not able to find the Dashboard class.

The solution is to create an empty Android project and compare the automatically generated .project and .properties files with the ones in your broken project.



回答9:

I was having the same problem but I had everything defined properly. I took another copy of the same project from my SVN and I was getting this error in the second project. Restart Eclipse: this will refresh eclipse's memory. It worked for me



回答10:

At last i solved it by making two android-support-v4.jar the same.. just copy any of the android-support-v4.jar in any of the projects related to your project and replace them..

using adding this to your android manifest

   <uses-library android:name="com.google.android.maps" />


回答11:

I found this post having similar question and a lot of different possible solutions. However, in my case the problem was in my Android project settings. Somehow my project's Java build path was not linked to the source folder. And thats why my manifest file was unable to find the activity class. I solved this problem by linking the src folder. To do so, right click on the project "properties"->"Java Build Path". Then select the tab "Source". Check if your src folder is listed here. If not, click on "Add Folder..." and then navigate to your src folder of the project.



回答12:

Today I had same issue and it was problem with the main class naming. It was IdleActivity and as soon as I changed it by setting IdleDnaActivity, application started successfuly.