NoClassDefFoundError:失败的解决方案:Landroid / support / v7 / appcompat / R $ styleable

时间:2014-08-21 15:44:53

标签: android android-activity android-studio android-appcompat

新手试图完成Google提供的我的第一个App教程。在通往这个致命异常的途中,我确实导入了很多随机软件包来摆脱"无法解决"许多事情的错误,ActionBarActivity,EditText,Fragment,LayoutInflater等,但不确定这是否重要。无论如何,当我点击"发送"我的应用程序崩溃并产生致命异常。主要活动中的按钮。这是我的代码和logcat文件。

MyActivity.java(又名教程的MainActivity.java)

package magiccoupons.tutapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


public class MyActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    // Do something in response to button
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}
}

DisplayMessageActivity.java:

package magiccoupons.tutapp;

import android.widget.*;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.*;
import android.support.v7.app.ActionBarActivity;
import android.app.Fragment;

public class DisplayMessageActivity extends ActionBarActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 *
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() { }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_display_message,
                container, false);
        return rootView;
    }
}
*/
}

的build.gradle:

 apply plugin: 'com.android.application'

android {
    compileSdkVersion 'android-L'
    buildToolsVersion '20'

    defaultConfig {
        applicationId "magiccoupons.tutapp"
        minSdkVersion 20
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:20.0.0'
    //compile 'com.android.support:appcompat-v7:21.0.0-rc1'
}

activity_my.xml:

    <LinearLayout 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:orientation="horizontal"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="magiccoupons.tutapp.MainActivity">

    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />

</LinearLayout>

activity_display_message.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="magiccoupons.tutapp.DisplayMessageActivity">

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

和logcat:

08-21 11:24:36.088    3645-3645/magiccoupons.tutapp I/Process﹕ Sending signal. PID: 3645 SIG: 9
08-21 11:37:33.584    4149-4149/magiccoupons.tutapp W/Resources﹕ Preloaded drawable resource #0x1080093 (android:drawable/sym_def_app_icon) that varies with configuration!!
08-21 11:37:33.676    4149-4149/magiccoupons.tutapp I/am_on_resume_called﹕ [0,magiccoupons.tutapp.MyActivity]
08-21 11:37:33.905    4149-4149/magiccoupons.tutapp D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
08-21 11:37:38.139    4149-4149/magiccoupons.tutapp I/am_on_paused_called﹕ [0,magiccoupons.tutapp.MyActivity]
08-21 11:37:38.218    4149-4149/magiccoupons.tutapp I/Choreographer﹕ Skipped 75 frames!  The application may be doing too much work on its main thread.
08-21 11:37:38.356    4149-4149/magiccoupons.tutapp D/AndroidRuntime﹕ Shutting down VM
08-21 11:37:38.369    4149-4149/magiccoupons.tutapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: magiccoupons.tutapp, PID: 4149
    java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/appcompat/R$styleable;
            at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:106)
            at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
            at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
            at magiccoupons.tutapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5720)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
            at android.app.ActivityThread.access$800(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5070)
            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:836)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.appcompat.R$styleable" on path: DexPathList[[zip file "/data/app/magiccoupons.tutapp-1.apk"],nativeLibraryDirectories=[/data/app-lib/magiccoupons.tutapp-1, /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.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:106)
            at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
            at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
            at magiccoupons.tutapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5720)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
            at android.app.ActivityThread.access$800(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5070)
            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:836)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
    Suppressed: java.lang.ClassNotFoundException: android.support.v7.appcompat.R$styleable
            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)
            ... 18 more
     Caused by: java.lang.NoClassDefFoundError: Class "Landroid/support/v7/appcompat/R$styleable;" not found
            ... 22 more

很抱歉,如果我错过了某些内容,这个问题完全没必要和/或是骗局。感谢。

5 个答案:

答案 0 :(得分:6)

由于以下原因,您收到该错误:

在您的Gradle构建文件中,您的应用正在定位并使用仍处于开发阶段的Android测试版进行编译:

compileSdkVersion 'android-L'
buildToolsVersion '20'

以及

minSdkVersion 20
targetSdkVersion 20

首先要注意的是,在没有安装android-L的任何设备上,此应用程序将无法正常运行(此时)。

您的问题的真正关键在于DisplayMessageActivity,它通过继承扩展 [ActionBarActivity] :( https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html)这是AppCompat的支持库类之一。

要解决此问题,请将您的最低SDK更改为10(或14,即冰淇淋三明治),将最大SDK更改为19(Kit Kat)并取消注释依赖项中的appcompat-v7库。

作为旁注,当您在各自的活动/片段中声明小部件时,将其范围置于任何方法之外通常是一种好习惯:

EditText editText;
Button sendMessageButton;

// Then in your onCreate() method
editText = (EditText) findViewById(R.id.editText);
sendMessageButton = (Button) findViewById(R.id.sendMessageButton);

这有助于减少内存重新分配并使代码更具可读性。有时您可能需要稍微改变规则,但这是常见做法。

答案 1 :(得分:3)

  

我删除了“yourAppName \ app”文件夹中的“build”文件夹

一切正常。如果上述解决方案不起作用,请尝试此操作。 再次构建项目时,将自动生成构建文件夹。

答案 2 :(得分:1)

Build->Clean Project
然后Build->Rebuild Project
就这些。

答案 3 :(得分:1)

就我而言,该问题发生在Android Studio 4.0中。我想Gradle缓存有问题。

不幸的是,Build -> Clean ProjectBuild -> Rebuild Projectfind . -wholename "*/build" -exec rm -rf {} \; 都没有帮助我,所以我不得不采取严厉措施。

  1. 关闭Android Studio。

  2. 打开终端并导航到项目的根目录。

  3. 删除所有Gradle模块的构建目录,以便尽快重新编译所有模块。

    rm -rf app/build
    

    但是,如果您只有一个模块,那么:

    rm -rf ~/.gradle
    
  4. 清除Gradle缓存。默认情况下,它位于您的主目录中。

    Archaea_Other
  5. 再次打开Android Studio。它将开始构建项目。由于我们刚刚删除了缓存,这将需要很长时间,但是该问题应得到解决。

答案 4 :(得分:0)

在我的情况下,我有一个为720p定义的布局,但没有为默认分辨率定义,因此崩溃了。添加该布局文件可解决此问题,logcat不会说谎。