-1

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

Community
  • 1
  • 1
Amandeep Rohila
  • 3,558
  • 2
  • 28
  • 34
  • asked so many times ... you did not setup this library properly ... go to http://d.android.com and find out how to install support lbrary for your IDE – Selvin Jun 09 '15 at 13:01
  • possible duplicate of [Android SnackBar Not Working](http://stackoverflow.com/questions/30612985/android-snackbar-not-working) – Bahman_Aries Jun 09 '15 at 13:04
  • i have put android-support-design.jar into libs folder -> build path -> Add to build path – Amandeep Rohila Jun 09 '15 at 13:06
  • no its not duplicated – Amandeep Rohila Jun 09 '15 at 13:07
  • your code is working fine. Just now checked it. I think the problem is only with library. Please ensure that you have added library properly – Chandru Jun 09 '15 at 13:09
  • *i have put android-support-design.jar into libs folder -> build path -> Add to build path* **it is not enough!!!** – Selvin Jun 09 '15 at 13:10
  • why are you using eclipse, btw? – njzk2 Jun 09 '15 at 13:12
  • Check this link: https://developer.android.com/tools/support-library/setup.html#libs-with-res And it is time to switch to Android Studio. – Gabriele Mariotti Jun 09 '15 at 13:13
  • yes i have already check it. in this link i have looked for Adding libraries without resources and it working fine at compile time. – Amandeep Rohila Jun 09 '15 at 13:17
  • @Selvin i have seen the documentation "Adding libraries without resources" from https://developer.android.com/tools/support-library/setup.html and found 1. Make sure you have downloaded the Android Support Library using the SDK Manager. 2. Create a libs/ directory in the root of your application project. 3. Copy the JAR file from your Android SDK installation directory (e.g., /extras/android/support/v4/android-support-v4.jar) into your application's project libs/ directory. 4. Right click the JAR file and select Build Path > Add to Build Path. – Amandeep Rohila Jun 09 '15 at 13:41
  • *I have seen the documentation "Adding libraries without resources"* guess what stand for R in `android/support/design/R$layout`... – Selvin Jun 09 '15 at 13:42
  • R is a class containing the definitions for all resources of a particular application package. but m not is getting what is base.apk from android.support.design.R$layout" on path: DexPathList[[zip file "/data/app/com.examples.androidmpreview-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] – Amandeep Rohila Jun 09 '15 at 13:44
  • 2
    @AmanRohila You have to check libraries with resources! – Gabriele Mariotti Jun 09 '15 at 23:04

2 Answers2

1

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.

Amandeep Rohila
  • 3,558
  • 2
  • 28
  • 34
0

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

Junaid
  • 3,477
  • 1
  • 24
  • 24