Android SnackBar giving exception and not working

3.8k views Asked by At

I have tried new Snackbar from the official design library provided by Google. I have downloaded new Design library through SDK and put it into libs folder. I am using eclipse and whenever I tried to run it it gives exception. I don't want to use any extra third party library other then Google.

java code

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>

Error

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

tried link

Android SnackBar: error inflating SnackbarLayout

2

There are 2 answers

0
Amandeep Rohila On BEST ANSWER

Solved! Firstly i removed the android-support-v4.jar from libs folder from project and then added the android-support-design library with resources[also added the support library]. After that project properties->Android->Add->Support-design library.

0
Junaid On

No need to remove android-support-v4.jar.

Right click on Project->BuildPath->Configure Buildpath Click on Libraries Tab and then click on Add external Libraries.

Select {path of adt}/sdk/extras/android/support/design/libs Select android-support-design.jar, Click Open to add this library.

Click Ok. This worked for me.

enter image description here