如何在eclipse中解决android项目的ClassNotFoundException?

时间:2015-07-30 03:19:11

标签: android eclipse android-studio

我有一个用Eclipse构建的android项目。我可以构建APK,但它会像以下日志一样崩溃:

E/AndroidRuntime(27898): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/pili/pldroid/streaming/camera/demo/CameraStreamingActivity;
E/AndroidRuntime(27898):    at com.example.rtmpdemo.MainActivity$1.onClick(MainActivity.java:35)
E/AndroidRuntime(27898):    at android.view.View.performClick(View.java:4757)
E/AndroidRuntime(27898):    at android.view.View$PerformClick.run(View.java:19757)
E/AndroidRuntime(27898):    at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime(27898):    at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(27898):    at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(27898):    at android.app.ActivityThread.main(ActivityThread.java:5258)
E/AndroidRuntime(27898):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(27898):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(27898):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
E/AndroidRuntime(27898):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
E/AndroidRuntime(27898): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.pili.pldroid.streaming.camera.demo.CameraStreamingActivity" on path: DexPathList[[zip file "/data/app/com.example.rtmpdemo-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.rtmpdemo-1/lib/arm, /vendor/lib, /system/lib]]
E/AndroidRuntime(27898):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E/AndroidRuntime(27898):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E/AndroidRuntime(27898):    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
E/AndroidRuntime(27898):    ... 11 more
E/AndroidRuntime(27898):    Suppressed: java.lang.NoClassDefFoundError: com.pili.pldroid.streaming.camera.demo.CameraStreamingActivity
E/AndroidRuntime(27898):        at dalvik.system.DexFile.defineClassNative(Native Method)
E/AndroidRuntime(27898):        at dalvik.system.DexFile.defineClass(DexFile.java:226)
E/AndroidRuntime(27898):        at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
E/AndroidRuntime(27898):        at dalvik.system.DexPathList.findClass(DexPathList.java:321)
E/AndroidRuntime(27898):        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
E/AndroidRuntime(27898):        ... 13 more
E/AndroidRuntime(27898):    Suppressed: java.lang.ClassNotFoundException: com.pili.pldroid.streaming.camera.demo.CameraStreamingActivity
E/AndroidRuntime(27898):        at java.lang.Class.classForName(Native Method)
E/AndroidRuntime(27898):        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
E/AndroidRuntime(27898):        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
E/AndroidRuntime(27898):        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
E/AndroidRuntime(27898):        ... 12 more
E/AndroidRuntime(27898):    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
W/ActivityManager(  810):   Force finishing activity com.example.rtmpdemo/.MainActivity

我已经绝对定义了CameraStreamingActivity并在AndroidManifest.xml中注册了。项目的结构如下:

./AndroidManifest.xml
./assets
./libs
./libs/android-support-v4.jar
./libs/armeabi-v7a
./libs/armeabi-v7a/libpldroid_ffmpegbridge.so
./libs/pldroid-camera-streaming-1.2.0.jar
./lint.xml
./proguard-project.txt
./project.properties
./src
./src/com
./src/com/example
./src/com/example/rtmpdemo
./src/com/example/rtmpdemo/MainActivity.java
./src/com/pili
./src/com/pili/pldroid
./src/com/pili/pldroid/streaming
./src/com/pili/pldroid/streaming/camera
./src/com/pili/pldroid/streaming/camera/demo
./src/com/pili/pldroid/streaming/camera/demo/AudioStreamingActivity.java
./src/com/pili/pldroid/streaming/camera/demo/CameraStreamingActivity.java
./src/com/pili/pldroid/streaming/camera/demo/StreamingBaseActivity.java
./src/com/pili/pldroid/streaming/camera/demo/TestActivity.java

AndroidManifest.xml:

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.rtmpdemo.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>
        <activity android:name="com.pili.pldroid.streaming.camera.demo.CameraStreamingActivity" >
        </activity>
        <activity
            android:name="com.pili.pldroid.streaming.camera.demo.AudioStreamingActivity"
            android:label="@string/title_activity_audio_streaming" >
        </activity>
        <activity android:name="com.pili.pldroid.streaming.camera.demo.TestActivity" >
        </activity>
    </application>

</manifest>

但是,将项目导入Android Studio后,它可以正常工作。

import-summary.txt如下:

ECLIPSE ANDROID PROJECT IMPORT SUMMARY
======================================

Ignored Files:
--------------
The following files were *not* copied into the new Gradle project; you
should evaluate whether these are still needed in your project and if
so manually move them:

* .DS_Store
* ic_launcher-web.png
* proguard-project.txt

Replaced Jars with Dependencies:
--------------------------------
The importer recognized the following .jar files as third party
libraries and replaced them with Gradle dependencies instead. This has
the advantage that more explicit version information is known, and the
libraries can be updated automatically. However, it is possible that
the .jar file in your project was of an older version than the
dependency we picked, which could render the project not compileable.
You can disable the jar replacement in the import wizard and try again:

android-support-v4.jar => com.android.support:support-v4:19.1.0

Moved Files:
------------
Android Gradle projects use a different directory structure than ADT
Eclipse projects. Here's how the projects were restructured:

* AndroidManifest.xml => app/src/main/AndroidManifest.xml
* assets/ => app/src/main/assets/
* libs/armeabi-v7a/libpldroid_ffmpegbridge.so => app/src/main/jniLibs/armeabi-v7a/libpldroid_ffmpegbridge.so
* libs/pldroid-camera-streaming-1.2.0.jar => app/libs/pldroid-camera-streaming-1.2.0.jar
* lint.xml => app/lint.xml
* res/ => app/src/main/res/
* src/ => app/src/main/java/

Next Steps:
-----------
You can now build the project. The Gradle project needs network
connectivity to download dependencies.

Bugs:
-----
If for some reason your project does not build, and you determine that
it is due to a bug or limitation of the Eclipse to Gradle importer,
please file a bug at http://b.android.com with category
Component-Tools.

(This import summary is for your information only, and can be deleted
after import once you are satisfied with the results.)

我错过了什么?

感谢。

1 个答案:

答案 0 :(得分:1)

实际上,您的代码没有任何问题。这是运行时错误。

我确定根本原因来自你错误导入的libs。 “./libs/pldroid-camera-streaming-1.2.0.jar” 尝试删除此lib并通过其他方式再次导入,例如:

方式1:Java构建路径 - &gt;图书馆 - &gt;添加外部JARS ..

方式2:手动将此lib直接复制到“.libs /”文件夹