【问题标题】:ClassNotFound when starting app启动应用程序时未找到 ClassNotFound
【发布时间】:2015-06-23 00:07:03
【问题描述】:

我正在尝试为 android wear 创建一个应用,但应用一启动我就收到此错误。

Process: com.demo.wear, PID: 20332
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.demo.wear/com.demo.wear.ui.WishesActivity}: java.lang.ClassNotFoundException: Didn't find class "com.demo.wear.ui.WishesActivity" on path: DexPathList[[zip file "/data/app/com.demo.wear-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.demo.wear.ui.WishesActivity" on path: DexPathList[[zip file "/data/app/com.demo.wear-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Suppressed: java.lang.NoClassDefFoundError: com.demo.wear.ui.WishesActivity
        at dalvik.system.DexFile.defineClassNative(Native Method)
        at dalvik.system.DexFile.defineClass(DexFile.java:226)
        at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
        at dalvik.system.DexPathList.findClass(DexPathList.java:321)
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
        ... 14 more
Suppressed: java.lang.ClassNotFoundException: com.demo.wear.ui.WishesActivity
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
        ... 13 more
 Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

我看到了很多关于这个主题的问题,但我找不到解决方案。

这里是清单和 gradle 文件,以备不时之需。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.demo.wear"
        minSdkVersion 21
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.support:wearable:1.2.0'
    compile 'com.google.android.gms:play-services-wearable:7.5.0'

    provided "org.projectlombok:lombok:1.16.4"

    compile 'com.jakewharton:butterknife:6.1.0'
}

还有清单

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo.wear" >

    <uses-feature android:name="android.hardware.type.watch" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.DeviceDefault" >
        <activity
            android:name="com.demo.wear.ui.WishesActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

这里是相关活动

package com.demo.wear.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.wearable.view.WearableListView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Asset;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.Wearable;
import com.demo.wear.R;
import com.demo.wear.models.Wish;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class WishesActivity extends Activity implements
        DataApi.DataListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, WearableListView.ClickListener {

    private static final String WISH_LIST_PATH = "/whishlist";
    private static final int RC_GOOGLE_API_CONNECTION_ERROR = 0;

    @InjectView(R.id.lstWishes_wishesActivity) WearableListView lstWishes;

    private GoogleApiClient googleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wishes);
        ButterKnife.inject(this);

        lstWishes.setAdapter(new WishesAdapter());
        lstWishes.setClickListener(WishesActivity.this);

        googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        googleApiClient.connect();
    }

    @Override
    protected void onStop() {
        Wearable.DataApi.removeListener(googleApiClient, this);
        googleApiClient.disconnect();
        super.onStop();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case RC_GOOGLE_API_CONNECTION_ERROR:
                if (resultCode == RESULT_OK) {
                    if (!googleApiClient.isConnecting() && !googleApiClient.isConnected()) {
                        googleApiClient.connect();
                    }
                }
                break;
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        Wearable.DataApi.addListener(googleApiClient, this);
    }

    @Override
    public void onConnectionSuspended(int i) {
        googleApiClient.connect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (connectionResult.hasResolution()) {
            try {
                connectionResult.startResolutionForResult(this, RC_GOOGLE_API_CONNECTION_ERROR);
            } catch (IntentSender.SendIntentException e) {
                googleApiClient.connect();
            }
        } else {
            Toast.makeText(this, "Error connecting with google API", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onDataChanged(DataEventBuffer dataEventBuffer) {
        for (DataEvent dataEvent : dataEventBuffer) {
            if (dataEvent.getDataItem().getUri().getPath().equals(WISH_LIST_PATH)) {
                DataMap dataMap = DataMapItem.fromDataItem(dataEvent.getDataItem()).getDataMap();
                List<Wish> wishes = new ArrayList<>();
                for (DataMap wishDataMap : dataMap.getDataMapArrayList("wishes")) {
                    Bitmap image = loadBitmapFromAsset(wishDataMap.getAsset("image"));
                    Wish wish = new Wish(wishDataMap.getString("name"), image);
                    wishes.add(wish);
                }

            }
        }
    }

    public Bitmap loadBitmapFromAsset(Asset asset) {
        ConnectionResult result = googleApiClient.blockingConnect(10000, TimeUnit.MILLISECONDS);
        if (!result.isSuccess()) {
            return null;
        }

        // convert asset into a file descriptor and block until it's ready
        InputStream assetInputStream = Wearable.DataApi.getFdForAsset(googleApiClient, asset).await().getInputStream();
        googleApiClient.disconnect();

        if (assetInputStream == null) {
            return null;
        }
        // decode the stream into a bitmap
        return BitmapFactory.decodeStream(assetInputStream);
    }

    @Override
    public void onClick(WearableListView.ViewHolder viewHolder) {
        Wish wish = (Wish) viewHolder.itemView.getTag();

    }

    @Override
    public void onTopEmptyRegionClick() {

    }
}

【问题讨论】:

  • 我将添加该类,但它甚至不会运行代码的第一行:(
  • 编译器正在以下文件夹中查找您的类:“/data/app/com.demo.wear-1/base.apk”。你确定它部署在那里?
  • 你必须使用 gradle.build 文件来导入你需要的类吗?几天前,我在尝试使用第三方库时遇到了这个问题。我为解决它所做的只是删除了我添加的所有 build.gradle 内容,然后将 .jar 文件下载到我的 libs 文件夹中。这对我来说似乎效果更好。然后只需确保您正在导入它。我会认为这更像是一种“替代”解决方案,而不是修复您在这里尝试做的事情。
  • @Tinki 我猜这是一个自动化的过程。我怎么能确定呢?
  • @dvsoukup 该应用程序正在正确构建,因此没有明确引用这些库。此外,为了确定,我在构建之前使缓存无效并清理了项目。但仍然没有:(

标签: android wear-os


【解决方案1】:

一个正常的原因是活动的android:name-property 中的包名称是错误的,但据我所知,在您的情况下它是正确的,我还查看了一些我自己的 gradle-/manifest -files,你的两个似乎都很好。

所以我建议尝试清理整个项目,然后完全重建它(假设您使用的是 Android Studio:Build->Clean Project 和 Build->Make Project)。这通常对我的情况有帮助。

【讨论】:

  • 感谢您的建议。我已经使缓存无效并清理了项目,但是……没有,仍然是同样的问题。
猜你喜欢
  • 1970-01-01
  • 2017-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
  • 2021-07-03
相关资源
最近更新 更多