Android SnackBar 出现异常且无法正常工作

Android SnackBar giving exception and not working

我尝试了 Google 提供的官方设计库中的新 Snackbar。我已经通过 SDK 下载了新的设计库并将其放入 libs 文件夹中。我正在使用 eclipse,每当我尝试 运行 它都会给出异常。除了 Google.

,我不想使用任何额外的第三方库

java代码

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends AppCompatActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "hello", Snackbar.LENGTH_LONG)
                        .setAction("Action!", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // NOP
                            }
                        })
                        .show();
            }
        });

    }

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: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="com.examples.androidmpreview.MainActivity"
    android:id="@+id/rl_parent" >

    <LinearLayout
        android:id="@+id/ll_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

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

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="104dp"
            android:layout_toRightOf="@+id/textView1"

            android:text="Button" />
    </LinearLayout>

</RelativeLayout>

错误

FATAL EXCEPTION: main
Process: com.examples.androidmpreview, PID: 26176
 java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/design/R$layout;
at android.support.design.widget.Snackbar.<init>(Snackbar.java:119)
at android.support.design.widget.Snackbar.make(Snackbar.java:140)
at com.examples.androidmpreview.MainActivity.onClick(MainActivity.java:20)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
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:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.R$layout" on path: DexPathList[[zip file "/data/app/com.examples.androidmpreview-2/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)
... 13 more
Suppressed: java.lang.ClassNotFoundException: android.support.design.R$layout
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)
... 14 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

尝试过link

已解决!首先,我从项目的 libs 文件夹中删除了 android-support-v4.jar,然后添加了带有资源的 android-support-design 库[也添加了支持库]。之后项目属性->Android->添加->支持设计库。

无需移除android-支持-v4.jar.

右击Project->BuildPath->Configure Buildpath 单击“库”选项卡,然后单击“添加外部库”。

Select {adt 的路径}/sdk/extras/android/support/design/libs Select android-support-design.jar,点击打开添加此库。

点击确定。这对我有用。