Android SnackBar提供异常而无法正常工作

时间:2015-06-09 12:59:08

标签: android android-layout android-design-library android-snackbar

我尝试过Google提供的官方设计库中的新Snackbar。我已经通过SDK下载了新的设计库并将其放入libs文件夹中。我正在使用eclipse,每当我尝试运行它时它会给出异常。我不想使用谷歌之外的任何额外的第三方库。

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$1.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

尝试了链接

Android SnackBar: error inflating SnackbarLayout

2 个答案:

答案 0 :(得分:1)

解决!首先,我从项目的libs文件夹中删除了android-support-v4.jar,然后添加了 android-support-design 库和资源[还添加了支持库]。在该项目属性之后 - > Android-&gt; Add-&gt;支持设计库。

答案 1 :(得分:0)

无需删除android-support-v4.jar。

右键单击Project-&gt; BuildPath-&gt;配置Buildpath 单击Libraries选项卡,然后单击Add external Libraries。

选择{adt}路径/ sdk / extras / android / support / design / libs 选择android-support-design.jar,单击“打开”添加此库。

单击“确定”。这对我有用。

enter image description here