我想从 Google 提供的官方设计库中试用新的 Snackbar。它的用法非常接近 Toast,所以我认为它很简单,可以尝试一下。
我已经在运行 5.1 的模拟器以及运行 5.0 的三星 Galaxy S6 Edge 上进行了尝试。
我的问题是应用程序在应该显示 Snackbar 时崩溃。
代码
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();
}
});
}
}
布局:
<?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>
摇篮文件
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'
}
我已经尝试提供不同的视图(root RelativeLayout、按钮视图和窗口的装饰视图)作为第一个参数,但无论我提供哪个,我仍然得到以下堆栈跟踪:
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