Android SnackBar:膨胀 SnackbarLayout 时出错
Android SnackBar: error inflating SnackbarLayout
我想试用 Google 提供的官方设计库中的新 Snackbar。它的用法非常接近Toast,所以我认为它很简单,可以尝试一下。
我已经在模拟器 运行 5.1 和 Samsung Galaxy S6 Edge 运行 5.0.
上试过了
我的问题是应用程序在应该显示 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>
Gradle 文件
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'
}
我尝试提供不同的视图(根 RelativeLayout、按钮视图和 window 的装饰视图)作为第一个参数,但无论我提供哪个,我仍然得到以下堆栈跟踪:
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
关于可能出错的两个想法。
首先,确保您使用的是正确的 gradle 依赖项:
compile 'com.android.support:design:22.2.0'
其次,确保您的 activity 扩展的是 AppCompatActivity
而不是 Activity
或 ActionBarActivity
。
您可以阅读 full post 了解更多详细信息,但这里是关于需要 AppCompat 与新的支持设计库的相关引用:
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.
如果您想知道原因,可以阅读 this or this 了解有关 Android 新的 "preferred" 处理回调方式的更多背景信息,特别是使用 AppCompatDelegate
。
我想试用 Google 提供的官方设计库中的新 Snackbar。它的用法非常接近Toast,所以我认为它很简单,可以尝试一下。
我已经在模拟器 运行 5.1 和 Samsung Galaxy S6 Edge 运行 5.0.
上试过了我的问题是应用程序在应该显示 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>
Gradle 文件
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'
}
我尝试提供不同的视图(根 RelativeLayout、按钮视图和 window 的装饰视图)作为第一个参数,但无论我提供哪个,我仍然得到以下堆栈跟踪:
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
关于可能出错的两个想法。
首先,确保您使用的是正确的 gradle 依赖项:
compile 'com.android.support:design:22.2.0'
其次,确保您的 activity 扩展的是 AppCompatActivity
而不是 Activity
或 ActionBarActivity
。
您可以阅读 full post 了解更多详细信息,但这里是关于需要 AppCompat 与新的支持设计库的相关引用:
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.
如果您想知道原因,可以阅读 this or this 了解有关 Android 新的 "preferred" 处理回调方式的更多背景信息,特别是使用 AppCompatDelegate
。