【问题标题】:Unable to instantiate application - ClassNotFoundException on MainActivity无法实例化应用程序 - MainActivity 上的 ClassNotFoundException
【发布时间】:2020-06-07 11:44:30
【问题描述】:

我已经尝试过多次清理我的项目,重新启动 Eclipse,转到构建路径 -> 导出,确保勾选了 Android 私有库,并且我尝试通过 XML 和 Java 文件进行解析以自己查找错误。我也尝试在 StackOverflow 上进行研究,但无济于事。

我还检查了我是否在 onCreate 方法之前调用了东西。

我担心我的清单 XML 文件可能会导致问题,但我认为它没有任何问题。

还有一些关于项目的信息,可以帮助您找到错误:

  1. Java 文件位于 Java 包 com.example.myfirstapp
  2. “Schedule”和“MainActivity”XML 文件位于layout 目录中。

由于错误与 MainActivity XML 文件有关,我将发布该文件和清单。

这是我的MainActivity XML 文件(文件名为 activity_main.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#800000"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">        
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/email"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:contentDescription="@+string/logo"
        android:src="@drawable/brock_logo"/>    
    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:ems="10"
        android:inputType="textWebPassword"/>    
    <EditText
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/password"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:inputType="textWebEmailAddress">
        <requestFocus />
    </EditText>
    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/password"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:onClick="login"
        android:text="@string/login" />    
</RelativeLayout>

这是我的AndroidManifest XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0">    
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19"/>    
    <uses-permission android:name="android.permission.INTERNET"/>    
    <application
        android:name=".MainActivity"
        android:allowBackup="true"
        android:hasCode="false"
        android:icon="@drawable/brock_logo"
        android:label="@string/app_name"
        android:logo="@drawable/brock_logo"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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>
        <activity
            android:name="com.example.myfirstapp.Schedule"
            android:label="@string/app_name"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity"/>
        </activity>
    </application>
</manifest>

这是我的Logcat 输出:

09-21 13:19:41.598: D/dalvikvm(1190): Late-enabling CheckJNI
09-21 13:19:41.690: D/AndroidRuntime(1190): Shutting down VM
09-21 13:19:41.690: W/dalvikvm(1190): threadid=1: thread exiting with uncaught exception    (group=0xa4d85b20)
09-21 13:19:41.698: E/AndroidRuntime(1190): FATAL EXCEPTION: main
09-21 13:19:41.698: E/AndroidRuntime(1190): Process: com.example.myfirstapp, PID: 1190
09-21 13:19:41.698: E/AndroidRuntime(1190): java.lang.RuntimeException: Unable to instantiate application com.example.myfirstapp.MainActivity: java.lang.ClassNotFoundException: Didn't find class "com.example.myfirstapp.MainActivity" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib]]
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.LoadedApk.makeApplication(LoadedApk.java:516)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4317)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.os.Looper.loop(Looper.java:136)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at java.lang.reflect.Method.invokeNative(Native Method)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at java.lang.reflect.Method.invoke(Method.java:515)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at dalvik.system.NativeStart.main(Native Method)
09-21 13:19:41.698: E/AndroidRuntime(1190): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.myfirstapp.MainActivity" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib]]
09-21 13:19:41.698: E/AndroidRuntime(1190):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.Instrumentation.newApplication(Instrumentation.java:975)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.LoadedApk.makeApplication(LoadedApk.java:511)
09-21 13:19:41.698: E/AndroidRuntime(1190):     ... 11 more

抱歉转储代码和调试输出。只是试图提供尽可能多的信息。

感谢阅读。

【问题讨论】:

  • 您已经为 Schedule 完全声明了包,而不是 MainActivity。你确定 Main Activity 在那个包里吗?

标签: android classnotfound


【解决方案1】:

试试这个:

 <application
    android:allowBackup="true"
    android:icon="@drawable/brock_logo"
    android:label="@string/app_name"
    android:logo="@drawable/brock_logo"
    android:theme="@style/AppTheme" >

然后清理并重建您的项目。

【讨论】:

    【解决方案2】:

    这告诉 Android 您要创建应用程序实例。您的应用程序中不需要该类。

    【讨论】:

    • 抱歉,我可能误解了您的意思。你是说我不需要 MainActivity.java 文件?
    【解决方案3】:

    我猜你实际上应该勾选使用 android 私有库。由于使用了错误的 appcompat 库,我也遇到了同样的问题。确保一切正常。

    【讨论】:

    • 对不起,我的错误,愚蠢的错字。输出和导出中的所有库都打勾。
    • 好吧,尝试右键单击您的 src 文件夹>构建路径>用作源。
    猜你喜欢
    • 2013-07-31
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 2020-02-24
    • 2011-08-17
    • 2012-04-26
    相关资源
    最近更新 更多