无法实例化活动...由 ClassNotFoundException 引起

发布于 2024-11-15 00:24:17 字数 4224 浏览 2 评论 0原文

将完美工作的应用程序转换为库(包括其 Activity 类!)后,我尝试通过简单地对库的活动进行超类化来创建使用整个库的应用程序:

package com.example.baseapp.paid;

import android.os.Bundle;
import com.example.baseapp.LibActivity;


public class PaidActivity extends LibActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

Eclipse 构建了这个新的“重新架构”的应用程序,没有任何错误,但是当我尝试运行它时,出现异常:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.baseapp.paid/com.example.baseapp.paid.PaidActivity}: java.lang.ClassNotFoundException: com.example.baseapp.paid.PaidActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.baseapp.paid-1.apk]
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.example.baseapp.paid.PaidActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.baseapp.paid-1.apk]
    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
    ... 11 more

我不知道为什么会发生这种情况,因为当该类正是构建的派生类(没有错误!)并且现在尝试运行时出现“未找到类”错误。

我该如何解决这个问题?

我缺少什么?

编辑(回答@CaspNZ的问题):

这是库项目的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.baseapp"
      android:versionCode="5"
      android:versionName="1.1.2"> 
    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET"/>   
</manifest>

这是使用库项目的应用程序的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.baseapp.paid"
      android:versionCode="5"
      android:versionName="1.1.2"> 
    <uses-sdk android:minSdkVersion="8" />
    <uses-library android:name="AppLibrary" />

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

    <application android:icon="@drawable/icon">
        <activity android:name=".PaidActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.baseapp.LibActivity" android:label="com.example.baseapp.LibActivity:string/rx_label">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="EditPreferences" 
                android:label="com.example.baseapp.LibActivity:string/app_name">
        </activity>
    </application>
</manifest>

我将不胜感激任何尝试的提示对此进行故障排除,因为这是我第一次尝试使用库项目,并且我不熟悉使其工作所需的“技巧”。

After converting a perfectly working application to a library (including its Activity class!), I am trying to create an application that uses that entire library by simply superclassing the library's activity:

package com.example.baseapp.paid;

import android.os.Bundle;
import com.example.baseapp.LibActivity;


public class PaidActivity extends LibActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

Eclipse builds this newly "re-architected" application without any errors, but when I try to run it, I get an exception:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.baseapp.paid/com.example.baseapp.paid.PaidActivity}: java.lang.ClassNotFoundException: com.example.baseapp.paid.PaidActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.baseapp.paid-1.apk]
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.example.baseapp.paid.PaidActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.baseapp.paid-1.apk]
    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
    ... 11 more

I have no idea why this is happening since the error for "Class Not Found" when the class is exactly the derived class built (without errors!) and now trying to run.

How do I troubleshoot this?

What am I missing?

EDIT (answering question by @CaspNZ):

This is the AndroidManifest.xml of the library project:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.baseapp"
      android:versionCode="5"
      android:versionName="1.1.2"> 
    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET"/>   
</manifest>

And this is the AndroidManifest.xml of the application, using the library project:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.baseapp.paid"
      android:versionCode="5"
      android:versionName="1.1.2"> 
    <uses-sdk android:minSdkVersion="8" />
    <uses-library android:name="AppLibrary" />

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

    <application android:icon="@drawable/icon">
        <activity android:name=".PaidActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.baseapp.LibActivity" android:label="com.example.baseapp.LibActivity:string/rx_label">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="EditPreferences" 
                android:label="com.example.baseapp.LibActivity:string/app_name">
        </activity>
    </application>
</manifest>

I would appreciate any hint trying to troubleshoot this as this is my first time ever trying to use a library project and I am not familiar with the "tricks" involved to get this working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仅此而已 2024-11-22 00:24:17

对于更新到 ADT 22 的任何人,请确保您已在 Java Build Path > Android Private Libraries 中选中 Android Private Libraries。订单和导出选项卡。如需了解更多信息,请阅读本文。这对我有用。也许没有什么力量将用户转移到 Android Studio 而不是 Eclipse:))

For anyone who updated to ADT 22, make sure that you have checked Android Private Libraries in the Java Build Path > Order and Export tab. For further information, read this. This works for me. Maybe little force to move users to Android Studio instead of Eclipse:))

枫林﹌晚霞¤ 2024-11-22 00:24:17

如果您在库清单中而不是在应用程序清单中列出活动,则可能会发生这种情况。无需进行<活动>库清单中的标签。 (例如,请参阅此线程。)

This can happen if you list the activities in the library manifest instead of in the application manifest. There's no need to have <activity> tags in a library manifest. (See, for instance, this thread.)

自找没趣 2024-11-22 00:24:17

我终于解决了这个问题。事实证明,我在两个项目的属性中都有 2 个关键设置未正确设置:

  1. 在库项目中,“Is Library”
    由于某种原因检查。
    我可以发誓我检查过了,但是
    知道Android有多么异想天开
    开发环境下
    Eclipse也可以,我怀疑是这样
    未被 Eclipse(或 ADT
    插件)由于一些故障而导致。
  2. 在应用项目中,我
    忽略添加我的图书馆项目
    通过添加...作为参考
    按钮。 (我能有多蠢?)

我希望像我这样的其他新手会发现这些信息有帮助。有时,一个极其难以调试的问题会隐藏配置错误,而无辜的毫无戒心的程序员会认为这已经被解决了......

I finally solved the problem. It turns out that I had 2 critical settings in the Properties of both projects not set correctly:

  1. In the library project, "Is Library"
    was not checked for some reason.
    I could swear that I checked it, but
    knowing how whimsical the Android
    development environment under
    Eclipse can be, I suspect that it
    was unchecked by Eclipse (or the ADT
    plugin) as a result of some glitch.
  2. In the application project, I
    neglected to add my library project
    as a reference via the Add...
    button. (how dumb could I be?)

I hope that other newbies like me will find this information helpful. Sometimes, an extremely difficult problem to debug, hides a configuration error when the innocent unsuspecting stressed programmer assumes that this is already has been taken care of...

甜宝宝 2024-11-22 00:24:17

我在我的一个项目中遇到了同样的错误,我想我可以分享我如何修复我的项目。

在我的项目的某个时刻,我必须更改我正在使用的所有 Activity 类所在的包名称之一。当我这样做时,我忘记更新 AndroidManifest,这导致了我同样的错误。更新这个修复了它。

I had the same error in one of my projects and thought that I can share how I fixed mine.

At some point of my project I had to change one of the package names I am using where all my Activity classes are situated. When I did, I forgot to update the AndroidManifest which caused me the same error. Updating this fixed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文