在 Android 中膨胀 class android.support.v7.widget.Toolbar 时出错
Error inflating class android.support.v7.widget.Toolbar in Android
我查看了大量 SO 问题并尝试实施它们。似乎无法理解这一点,昨天还可以,但现在不行了。
gradle:
android {
compileSdkVersion 23
buildToolsVersion "23.0.0 "
dexOptions {
javaMaxHeapSize "4g"
}
defaultConfig {
minSdkVersion 16
multiDexEnabled true
targetSdkVersion 23
}
dependencies {
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:cardview-v7:23.0.0'
compile 'com.android.support:design:23+'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:recyclerview-v7:23.0.0'
compile 'com.android.support:support-v4:24.0.0-alpha2'
compile 'com.github.fernandodev.easyratingdialog:easyratingdialog:1.1.0'
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.parse.bolts:bolts-android:1.+'
compile 'com.parse:parse-android:1.+'
compile 'com.squareup:otto:1.3.8'
compile 'com.moengage:moe-android-sdk:6.0.10'
compile 'com.amazonaws:aws-android-sdk-mobileanalytics:2.2.9'
compile 'org.apmem.tools:layouts:1.9@aar'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile('com.crashlytics.sdk.android:answers:1.3.6@aar') {
transitive = true;
}
compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
transitive = true;
}
}
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
android:background="@color/colorPrimary"
android:elevation="4dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="@dimen/abc_action_bar_default_height_material"
android:theme="@style/ThemeOverlay.AppCompat.Dark"/>
BaseActivity.class
public abstract class BaseActivity extends AppCompatActivity {
private MoEHelper mHelper;
public static MobileAnalyticsManager analytics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHelper = new MoEHelper(this);
try {
analytics = MobileAnalyticsManager.getOrCreateInstance(
this.getApplicationContext(),
"", //Amazon Mobile Analytics App ID
"" //Amazon Cognito Identity Pool ID
);
} catch(InitializationException ex) {
Log.e(this.getClass().getName(), "Failed to initialize Amazon Mobile Analytics", ex);
}
}
@Override
protected void onStart() {
super.onStart();
mHelper.onStart(this);
}
@Override
protected void onStop() {
super.onStop();
mHelper.onStop(this);
}
@Override
protected void onPause() {
super.onPause();
mHelper.onPause(this);
if(analytics != null) {
analytics.getSessionClient().pauseSession();
//Attempt to send any events that have been recorded to the Mobile Analytics service.
analytics.getEventClient().submitEvents();
}
}
@Override
protected void onResume() {
super.onResume();
mHelper.onResume(this);
if(analytics != null) {
analytics.getSessionClient().resumeSession();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mHelper.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mHelper.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
mHelper.onNewIntent(this, intent);
}
}
Activity class
public abstract class Activity extends BaseActivity
{
protected Toolbar toolbar;
// region Activity Lifecycle
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(getContentView());
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null)
{
setSupportActionBar(toolbar);
}
}
// endregion
// region Getters/Setters
protected String getActionBarTitle()
{
String actionBarTitle = "";
ActionBar actionBar = getSupportActionBar();
if (actionBar != null && actionBar.getTitle() != null)
{
actionBarTitle = actionBar.getTitle().toString();
}
return actionBarTitle;
}
protected void setActionBarTitle(int resourceId)
{
String actionBarTitle = getString(resourceId);
setActionBarTitle(actionBarTitle);
}
protected void setActionBarTitle(String actionBarTitle)
{
ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
{
if (!StringUtilities.isNullOrEmpty(actionBarTitle))
{
SpannableString spannableString = new SpannableString(actionBarTitle);
spannableString.setSpan(new TypefaceSpan("sans-serif-light"), 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.setTitle(spannableString);
actionBar.setDisplayShowTitleEnabled(true);
}
else
{
actionBar.setDisplayShowTitleEnabled(false);
}
}
}
// endregion
abstract protected int getContentView();
}
Activity_main.xml
Logcat
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
at android.view.LayoutInflater.createView(LayoutInflater.java:640)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:904)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.app.Activity.onCreate(Activity.java:24)
at com.app.DrawerActivity.onCreate(DrawerActivity.java:51)
at com.app.MainActivity.onCreate(MainActivity.java:86)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.access0(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
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:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
我过去也遇到过类似的问题。 android:theme 是工具栏或 AppBarLayout 上的问题。
编辑:代码示例
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
选项2:
<style name="MyToolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:background">@color/colorPrimary</item>
</style>
<style name="AppTheme" parent="@style/Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
试试这个主题片段
这是对我特别有用的。我的问题与主题无关,而是依赖项和 targetSdkVersion 的正确编译版本。更新了下面的 gradle 文件。更改了
原版Gradle
compile 'com.android.support:support-v4:24.0.0-alpha2'
已更改:
compile 'com.android.support:support-v4:23.0.0'
基本上问题是我的这个版本的库与其他版本和 minsdk 版本不匹配,所以有些东西没有很好地融合。一旦我更新了所有依赖项(特别是来自 google 设计库)以相互匹配,它就起作用了。
我查看了大量 SO 问题并尝试实施它们。似乎无法理解这一点,昨天还可以,但现在不行了。
gradle:
android {
compileSdkVersion 23
buildToolsVersion "23.0.0 "
dexOptions {
javaMaxHeapSize "4g"
}
defaultConfig {
minSdkVersion 16
multiDexEnabled true
targetSdkVersion 23
}
dependencies {
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:cardview-v7:23.0.0'
compile 'com.android.support:design:23+'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:recyclerview-v7:23.0.0'
compile 'com.android.support:support-v4:24.0.0-alpha2'
compile 'com.github.fernandodev.easyratingdialog:easyratingdialog:1.1.0'
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.parse.bolts:bolts-android:1.+'
compile 'com.parse:parse-android:1.+'
compile 'com.squareup:otto:1.3.8'
compile 'com.moengage:moe-android-sdk:6.0.10'
compile 'com.amazonaws:aws-android-sdk-mobileanalytics:2.2.9'
compile 'org.apmem.tools:layouts:1.9@aar'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile('com.crashlytics.sdk.android:answers:1.3.6@aar') {
transitive = true;
}
compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
transitive = true;
}
}
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
android:background="@color/colorPrimary"
android:elevation="4dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="@dimen/abc_action_bar_default_height_material"
android:theme="@style/ThemeOverlay.AppCompat.Dark"/>
BaseActivity.class
public abstract class BaseActivity extends AppCompatActivity {
private MoEHelper mHelper;
public static MobileAnalyticsManager analytics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHelper = new MoEHelper(this);
try {
analytics = MobileAnalyticsManager.getOrCreateInstance(
this.getApplicationContext(),
"", //Amazon Mobile Analytics App ID
"" //Amazon Cognito Identity Pool ID
);
} catch(InitializationException ex) {
Log.e(this.getClass().getName(), "Failed to initialize Amazon Mobile Analytics", ex);
}
}
@Override
protected void onStart() {
super.onStart();
mHelper.onStart(this);
}
@Override
protected void onStop() {
super.onStop();
mHelper.onStop(this);
}
@Override
protected void onPause() {
super.onPause();
mHelper.onPause(this);
if(analytics != null) {
analytics.getSessionClient().pauseSession();
//Attempt to send any events that have been recorded to the Mobile Analytics service.
analytics.getEventClient().submitEvents();
}
}
@Override
protected void onResume() {
super.onResume();
mHelper.onResume(this);
if(analytics != null) {
analytics.getSessionClient().resumeSession();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mHelper.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mHelper.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
mHelper.onNewIntent(this, intent);
}
}
Activity class
public abstract class Activity extends BaseActivity
{
protected Toolbar toolbar;
// region Activity Lifecycle
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(getContentView());
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null)
{
setSupportActionBar(toolbar);
}
}
// endregion
// region Getters/Setters
protected String getActionBarTitle()
{
String actionBarTitle = "";
ActionBar actionBar = getSupportActionBar();
if (actionBar != null && actionBar.getTitle() != null)
{
actionBarTitle = actionBar.getTitle().toString();
}
return actionBarTitle;
}
protected void setActionBarTitle(int resourceId)
{
String actionBarTitle = getString(resourceId);
setActionBarTitle(actionBarTitle);
}
protected void setActionBarTitle(String actionBarTitle)
{
ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
{
if (!StringUtilities.isNullOrEmpty(actionBarTitle))
{
SpannableString spannableString = new SpannableString(actionBarTitle);
spannableString.setSpan(new TypefaceSpan("sans-serif-light"), 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.setTitle(spannableString);
actionBar.setDisplayShowTitleEnabled(true);
}
else
{
actionBar.setDisplayShowTitleEnabled(false);
}
}
}
// endregion
abstract protected int getContentView();
}
Activity_main.xml
Logcat
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
at android.view.LayoutInflater.createView(LayoutInflater.java:640)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:904)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.app.Activity.onCreate(Activity.java:24)
at com.app.DrawerActivity.onCreate(DrawerActivity.java:51)
at com.app.MainActivity.onCreate(MainActivity.java:86)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.access0(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
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:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
我过去也遇到过类似的问题。 android:theme 是工具栏或 AppBarLayout 上的问题。
编辑:代码示例
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
选项2:
<style name="MyToolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:background">@color/colorPrimary</item>
</style>
<style name="AppTheme" parent="@style/Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
试试这个主题片段
这是对我特别有用的。我的问题与主题无关,而是依赖项和 targetSdkVersion 的正确编译版本。更新了下面的 gradle 文件。更改了
原版Gradle
compile 'com.android.support:support-v4:24.0.0-alpha2'
已更改:
compile 'com.android.support:support-v4:23.0.0'
基本上问题是我的这个版本的库与其他版本和 minsdk 版本不匹配,所以有些东西没有很好地融合。一旦我更新了所有依赖项(特别是来自 google 设计库)以相互匹配,它就起作用了。