I wanted to try out the new Snackbar from the official design library provided by Google. Its usage is very close to a Toast so I thought it would be simple enough to try out.
I've tried it out on an emulator running 5.1 as well as Samsung Galaxy S6 Edge running 5.0.
My problem is that the app crashes when it's supposed to display the Snackbar.
Code
package com.jayway.andreas.test;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, R.string.test, Snackbar.LENGTH_LONG)
.setAction("Action!", new View.OnClickListener() {
@Override
public void onClick(View v) {
// NOP
}
})
.show();
}
});
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/root_layout"
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=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Button"/>
</RelativeLayout>
Gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0 rc1"
defaultConfig {
applicationId "com.jayway.andreas.test"
minSdkVersion 21
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:design:22.2.0'
}
I have tried providing different views(root RelativeLayout, view of button and the window's decor view) as the first parameter, but no matter which one I've provided I still get the following stacktrace:
android.view.InflateException: Binary XML file line #18: Error inflating class android.support.design.widget.Snackbar$SnackbarLayout
E/AndroidRuntime(19107): at android.view.LayoutInflater.createView(LayoutInflater.java:640)
E/AndroidRuntime(19107): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
E/AndroidRuntime(19107): at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
E/AndroidRuntime(19107): at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
E/AndroidRuntime(19107): at android.support.design.widget.Snackbar.<init>(Snackbar.java:119)
E/AndroidRuntime(19107): at android.support.design.widget.Snackbar.make(Snackbar.java:140)
E/AndroidRuntime(19107): at android.support.design.widget.Snackbar.make(Snackbar.java:164)
E/AndroidRuntime(19107): at com.jayway.andreas.test.MainActivitykr8.51onClick(MainActivity.java:20)
E/AndroidRuntime(19107): at android.view.View.performClick(View.java:5217)
E/AndroidRuntime(19107): at android.view.View$PerformClick.run(View.java:20983)
E/AndroidRuntime(19107): at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime(19107): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(19107): at android.os.Looper.loop(Looper.java:145)
E/AndroidRuntime(19107): at android.app.ActivityThread.main(ActivityThread.java:6141)
E/AndroidRuntime(19107): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(19107): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(19107): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
E/AndroidRuntime(19107): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
E/AndroidRuntime(19107): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime(19107): at java.lang.reflect.Constructor.newInstance(Native Method)
E/AndroidRuntime(19107): at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
E/AndroidRuntime(19107): at android.view.LayoutInflater.createView(LayoutInflater.java:614)
E/AndroidRuntime(19107): ... 17 more
E/AndroidRuntime(19107): Caused by: android.view.InflateException: Binary XML file line #34: Error inflating class <unknown>
E/AndroidRuntime(19107): at android.view.LayoutInflater.createView(LayoutInflater.java:640)
E/AndroidRuntime(19107): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
E/AndroidRuntime(19107): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
E/AndroidRuntime(19107): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
E/AndroidRuntime(19107): at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
E/AndroidRuntime(19107): at android.view.LayoutInflater.inflate(LayoutInflater.java:480)
E/AndroidRuntime(19107): at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
E/AndroidRuntime(19107): at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
E/AndroidRuntime(19107): at android.support.design.widget.Snackbar$SnackbarLayout.<init>(Snackbar.java:526)
E/AndroidRuntime(19107): ... 20 more
E/AndroidRuntime(19107): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime(19107): at java.lang.reflect.Constructor.newInstance(Native Method)
E/AndroidRuntime(19107): at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
E/AndroidRuntime(19107): at android.view.LayoutInflater.createView(LayoutInflater.java:614)
E/AndroidRuntime(19107): ... 28 more
E/AndroidRuntime(19107): Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 13
E/AndroidRuntime(19107): at android.content.res.TypedArray.getDrawable(TypedArray.java:760)
E/AndroidRuntime(19107): at android.view.View.<init>(View.java:3990)
E/AndroidRuntime(19107): at android.widget.TextView.<init>(TextView.java:1021)
E/AndroidRuntime(19107): at android.widget.TextView.<init>(TextView.java:1016)
E/AndroidRuntime(19107): at android.widget.TextView.<init>(TextView.java:1012)
E/AndroidRuntime(19107): ... 31 more
Two thoughts on what could be wrong.
First, make sure you are using the correct gradle dependency:
compile 'com.android.support:design:22.2.0'
Second, make sure your activity is extending AppCompatActivity
and not Activity
or ActionBarActivity
.
You can read the full post for more details, but here's the relevant quote about needing AppCompat with the new Support Design library:
Note that as the Design library depends on the Support v4 and AppCompat Support Libraries, those will be included automatically when you add the Design library dependency.
If you want to know why, you can read this or this for more background about Android's new "preferred" way of handling callbacks, specifically using AppCompatDelegate
.