ClassNotFoundException when the class is there?

2019-03-05 08:15发布

问题:

I'm a bit stuck on this. Android is throwing a ClassNotFoundException even though there is such a class.

04-20 09:07:50.179: E/AndroidRuntime(525): FATAL EXCEPTION: main
04-20 09:07:50.179: E/AndroidRuntime(525): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.company.app/com.company.app.Main}: java.lang.ClassNotFoundException: com.company.app.Main
04-20 09:07:50.179: E/AndroidRuntime(525):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
04-20 09:07:50.179: E/AndroidRuntime(525):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-20 09:07:50.179: E/AndroidRuntime(525):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-20 09:07:50.179: E/AndroidRuntime(525):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-20 09:07:50.179: E/AndroidRuntime(525):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 09:07:50.179: E/AndroidRuntime(525):  at android.os.Looper.loop(Looper.java:137)
04-20 09:07:50.179: E/AndroidRuntime(525):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-20 09:07:50.179: E/AndroidRuntime(525):  at java.lang.reflect.Method.invokeNative(Native Method)
04-20 09:07:50.179: E/AndroidRuntime(525):  at java.lang.reflect.Method.invoke(Method.java:511)
04-20 09:07:50.179: E/AndroidRuntime(525):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-20 09:07:50.179: E/AndroidRuntime(525):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-20 09:07:50.179: E/AndroidRuntime(525):  at dalvik.system.NativeStart.main(Native Method)
04-20 09:07:50.179: E/AndroidRuntime(525): Caused by: java.lang.ClassNotFoundException: com.company.app.Main
04-20 09:07:50.179: E/AndroidRuntime(525):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
04-20 09:07:50.179: E/AndroidRuntime(525):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-20 09:07:50.179: E/AndroidRuntime(525):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-20 09:07:50.179: E/AndroidRuntime(525):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
04-20 09:07:50.179: E/AndroidRuntime(525):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
04-20 09:07:50.179: E/AndroidRuntime(525):  ... 11 more

There is such a class called Main in Main.java.

public class Main extends FragmentActivity which is in the package com.company.app.

It was working before but when I went back to work on the app today it just stopped working. I don't know what happened.

It is defined correctly in the Manifest.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.company.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Light" >
        <activity
            android:label="@string/app_name"
            android:name=".Main" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Few things I've tried:

  • Restarting Eclipse
  • Restarting the emulator
  • Uninstalling and reinstalling the app in the emulator
  • Cleaning the project.
  • Checking if gen and src is in the Source tab of the build path.

As it looks right now, I may have to abandon the current project and start from scratch.

Any help would be much appreciated.

UPDATE:

The app works if Main extends Activity instead of FragmentActivity. Perhaps it is something with the Android Support package?

UPDATE 2:

I'm pretty sure it is. When I don't use the support package and I use the normal Fragment classes in Android 3.0 and up, the app works fine.


The problem is now fixed. :)

For some reason the Android support package removed itself from the /libs directory in the project and it was referencing it from an external location (outside the workspace directory).

I will mark Abdu Egal's first answer as correct as he directed me on the right track.

回答1:

It seems that there is an error in the onCreate of your main activity. Did you provide an onCreate(Bundle savedInstance) if so , try to clear it and see if it works then.

If that does not work, try to use extend Activity instead of extend FrameActivity.

Good luck



回答2:

From your quition detail i assume that you forgot to declare your FrameActivity into AndroidManifest.xml , kindly check your AndroidManifest.xml file entry.



回答3:

I think you used the FragmentActivity the wrong way, I guess FragmentActivities should be a part of a normal activity, see below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <fragment
        android:name="com.mamlambo.tutorial.tutlist.TutListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/tutlist_fragment"
        android:layout_weight="45">
    </fragment>
    <fragment
        android:name="com.mamlambo.tutorial.tutlist.TutViewerFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/tutview_fragment"
        android:layout_weight="55">
    </fragment>
</LinearLayout>

Taken from this link (part 9)

http://mobile.tutsplus.com/tutorials/android/android-sdk_fragments/



回答4:

Right click on your project folder => Properties => Java Build Path => Order and Export => check the box "Android Private Libraries" if not checked. Make a clean. And it works !



回答5:

I got the same problem with MobilTuts sample, problem was with support library. I have changed the build target to a higher API than 10, something like 15 for example, and then all errors vanished.

Project Properties --> Android --> Project Build Target --> Set it to 15 .e.g

Recommendation: set MIN API inside manifest to 8 at lest.