为什么我仍然收到 java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$attr? [英] Why am I still getting java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$attr?

查看:28
本文介绍了为什么我仍然收到 java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$attr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在开发一个俗气的应用程序,以便在我从事我公司的真正应用程序项目之前发布帖子以在 android 中训练自己.我遇到了一个让我非常困难的错误.

So I'm developing a cheesy app to make posts to train myself in android before I work on my firm's real app project. I'm running into a bug that's giving me a very difficult time though.

我得到了一个我要使用的库依赖项列表,并设置了我的 build.gradle 文件以加载它们.它同步得很好,所以我认为这是我所知道的最好的:

I was given a list of library dependencies that I was to use, and have my build.gradle file set up to load them in. It syncs fine, so I assume it's done right to the best of my knowledge:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.google.android.gms:play-services:7.3.0'
    compile 'com.squareup.okhttp:okhttp:2.3.0'
    compile 'com.squareup.retrofit:retrofit:1.7.0'
    compile 'com.google.code.gson:gson:2.3'
    compile 'com.squareup.dagger:dagger:1.2.2'
    compile 'com.squareup.dagger:dagger-compiler:1.2.2'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.android.support:recyclerview-v7:22.1.1'
    compile 'com.squareup:otto:1.3.7'
    compile 'com.path:android-priority-jobqueue:1.1.2'
    compile 'io.realm:realm-android:0.80.1'
    compile 'com.jakewharton:butterknife:6.1.0'
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.android.support:appcompat-v7:22.1.1'
}

而且我实际上已经安装了 android 支持库和存储库:

And I have actually installed the android support library AND repository:

但我仍然尝试启动应用程序并获得以下堆栈跟踪:

And yet I still try and launch the app and get the following stack trace:

java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$attr
        at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:286)
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:246)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
        at com.anglersatalas.twitstagram.MainActivity.onCreate(MainActivity.java:23)
        at android.app.Activity.performCreate(Activity.java:5451)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
        at android.app.ActivityThread.access$900(ActivityThread.java:169)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5487)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)

我见过的 Stack Overflow 上的其他任何地方都有解决方案,即添加 appcompat v7 库,正如他们在 android 网站上所说的那样,我已经在上面演示了我已经完成的操作.我一生都无法弄清楚为什么这仍然会发生.查看堆栈跟踪,它在 setContentView() 方法中中断了我的 MainActivity:

Everywhere else on Stack Overflow I've seen has the solution as adding the appcompat v7 libraries as they say to do on the android website, which I've demonstrated I've done above. I can't figure out for the life of me why this is still happening. Looking at the stack trace, it breaks in my MainActivity at the setContentView() method:

package com.anglersatalas.twitstagram;

import android.os.Bundle;
import com.anglersatalas.twitstagram.R;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.anglersatalas.twitstagram.api.PostService;
import com.anglersatalas.twitstagram.models.Post;

import java.util.List;

import retrofit.RestAdapter;


public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.anglersatalas.twitstagram.R.layout.activity_main);

        TextView tv =  (TextView)findViewById(com.anglersatalas.twitstagram.R.id.textView);

        RestAdapter adapter = new RestAdapter.Builder()
                .setEndpoint("10.0.0.248/testserver/backend/web")
                .build();

        PostService service = adapter.create(PostService.class);
        List<Post> list = service.getPosts();
        String str = "";
        for(Post p : list){
            str += String.format("%s
", p);
        }
        tv.setText(str);

    }

    ...

}

点击调用抛出异常的方法的类文件,显示找不到'android.support.v7.appcompat.R;':

Clicking through to the class file that is calling the method that throws the exception, it shows that it can't find 'android.support.v7.appcompat.R;':

我和我的同事都不知所措.有关如何解决此问题的任何建议?

My coworkers and I are at a loss. Any suggestions as to how to remedy this?

推荐答案

我刚刚花了几个小时解决同样的问题,我终于解决了...

I just spent hours with this same issue, and I was able to get it resolved...

事实证明我超过了 dex 限制,我的 build.gradle 文件中的 defaultConfig 确实有 multiDexEnabled 为真,但对于 5.0 之前的设备,我似乎缺少其他一些东西..

It turns out I was over the dex limit, I did have multiDexEnabled true in my build.gradle file under defaultConfig, but it looks like I was missing a few other things for pre 5.0 devices..

defaultConfig { 
...
        multiDexEnabled true 
... }

我首先用这个检查了我的方法计数:https://github.com/mihaip/dex-方法计数

I first checked my method count with this: https://github.com/mihaip/dex-method-counts

我在 67952 方法...

I was at 67952 methods...

然后我更新了我的 build.gradle 以添加 multidex 支持

I then updated my build.gradle to add multidex support

dependencies {
...
compile 'com.android.support:multidex:1.0.0'
... }

然后我更新了我的应用程序(我们已经有了自己的应用程序)以从 MultiDexApplication 扩展

I then updated my Application (we already have our own) to extend from MultiDexApplication

package com.me.myapp
...
public class Application extends MultiDexApplication {}

这是我用来正确实现 multidex 的文档中的两个链接:https://developer.android.com/tools/building/multidex.htmlhttps://developer.android.com/reference/android/support/multidex/MultiDexApplication.html

Here were the two links from the documentation I used to correctly implement multidex: https://developer.android.com/tools/building/multidex.html https://developer.android.com/reference/android/support/multidex/MultiDexApplication.html

确保清单中的应用程序指向您的自定义 multidex 应用程序

Make sure your application in the manifest points to your custom multidex application

<application
...
android:name="com.me.myapp.Application" />

这篇关于为什么我仍然收到 java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$attr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆