InflateException 膨胀类 android.support.design.widget.CoordinatorLayout 时出错
Posted
技术标签:
【中文标题】InflateException 膨胀类 android.support.design.widget.CoordinatorLayout 时出错【英文标题】:InflateException Error inflating class android.support.design.widget.CoordinatorLayout 【发布时间】:2017-02-20 01:15:25 【问题描述】:E/androidRuntime: 致命异常: main 进程:com.atech.a_business,PID:30662 java.lang.RuntimeException:无法启动活动 ComponentInfocom.atech.a_business/com.atech.a_business.layout.MainActivity:android.view.InflateException:二进制 XML 文件第 2 行:错误膨胀类 android.support.design .widget.CoordinatorLayout 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3124) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233) 在 android.app.ActivityThread.access$1000(ActivityThread.java:197) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:145) 在 android.app.ActivityThread.main(ActivityThread.java:6873) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 引起:android.view.InflateException:二进制 XML 文件第 2 行:错误膨胀类 android.support.design.widget.CoordinatorLayout 在 android.view.LayoutInflater.createView(LayoutInflater.java:640) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 在 android.view.LayoutInflater.parseInclude(LayoutInflater.java:904) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:511) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:415) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:366) 在 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 在 android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 在 com.atech.a_business.layout.MainActivity.onCreate(MainActivity.java:69) 在 android.app.Activity.performCreate(Activity.java:6550) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3077) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233) 在 android.app.ActivityThread.access$1000(ActivityThread.java:197) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:145) 在 android.app.ActivityThread.main(ActivityThread.java:6873) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 引起:java.lang.reflect.InvocationTargetException 在 java.lang.reflect.Constructor.newInstance(Native Method) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:288) 在 android.view.LayoutInflater.createView(LayoutInflater.java:614) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 在 android.view.LayoutInflater.parseInclude(LayoutInflater.java:904) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:511) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:415) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:366) 在 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 在 android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 在 com.atech.a_business.layout.MainActivity.onCreate(MainActivity.java:69) 在 android.app.Activity.performCreate(Activity.java:6550) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3077) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233) 在 android.app.ActivityThread.access$1000(ActivityThread.java:197) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:145) 在 android.app.ActivityThread.main(ActivityThread.java:6873) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 引起:java.lang.IllegalArgumentException:您需要将 Theme.AppCompat 主题(或后代)与设计库一起使用。 在 android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:36) 在 android.support.design.widget.CoordinatorLayout.(CoordinatorLayout.java:192) 在 android.support.design.widget.CoordinatorLayout.(CoordinatorLayout.java:186) 在 java.lang.reflect.Constructor.newInstance(Native Method) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:288) 在 android.view.LayoutInflater.createView(LayoutInflater.java:614) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 在 android.view.LayoutInflater.parseInclude(LayoutInflater.java:904) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:511) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:415) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:366) 在 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 在 android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 在 com.atech.a_business.layout.MainActivity.onCreate(MainActivity.java:69) 在 android.app.Activity.performCreate(Activity.java:6550) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3077) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3233) 在 android.app.ActivityThread.access$1000(ActivityThread.java:197) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:145) 在 android.app.ActivityThread.main(ActivityThread.java:6873) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
CoordinatorLayout xml 文件:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_
android:layout_>
<android.support.design.widget.AppBarLayout
android:layout_
android:layout_
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/frame"
android:layout_
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior"></FrameLayout>
</android.support.design.widget.CoordinatorLayout>
主要活动 .xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_
android:layout_
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_
android:layout_ />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
主要活动类
package com.atech.a_business.layout;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.atech.a_business.R;
import com.atech.a_business.layout.fragments.HomeFragment;
import com.atech.a_business.layout.fragments.MoviesFragment;
import com.atech.a_business.layout.fragments.NotificationsFragment;
import com.atech.a_business.layout.fragments.PhotosFragment;
import com.atech.a_business.layout.fragments.SettingsFragment;
import com.atech.a_business.layout.settings.AboutUsActivity;
import com.atech.a_business.util.CircleTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
public class MainActivity extends AppCompatActivity
private NavigationView navigationView;
private DrawerLayout drawer;
private View navHeader;
private ImageView imgNavHeaderBg, imgProfile;
private TextView txtName, txtWebsite;
private Toolbar toolbar;
//private FloatingActionButton fab;
// urls to load navigation header background image
// and profile image
private static final String urlNavHeaderBg = "http://api.androidhive.info/images/nav-menu-header-bg.jpg";
private static final String urlProfileImg = "https://lh3.googleusercontent.com/eCtE_G34M9ygdkmOpYvCag1vBARCmZwnVS6rS5t4JLzJ6QgQSBquM0nuTsCpLhYbKljoyS-txg";
// index to identify current nav menu item
public static int navItemIndex = 0;
// tags used to attach the fragments
private static final String TAG_HOME = "home";
private static final String TAG_PHOTOS = "photos";
private static final String TAG_MOVIES = "movies";
private static final String TAG_NOTIFICATIONS = "notifications";
private static final String TAG_SETTINGS = "settings";
public static String CURRENT_TAG = TAG_HOME;
// toolbar titles respected to selected nav menu item
private String[] activityTitles;
// flag to load home fragment when user presses back key
private boolean shouldLoadHomeFragOnBackPress = true;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mHandler = new Handler();
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
//fab = (FloatingActionButton) findViewById(R.id.fab);
// Navigation view header
navHeader = navigationView.getHeaderView(0);
txtName = (TextView) navHeader.findViewById(R.id.name);
txtWebsite = (TextView) navHeader.findViewById(R.id.website);
imgNavHeaderBg = (ImageView) navHeader.findViewById(R.id.img_header_bg);
imgProfile = (ImageView) navHeader.findViewById(R.id.img_profile);
// load toolbar titles from string resources
activityTitles = getResources().getStringArray(R.array.nav_item_activity_titles);
/*fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
);*/
// load nav menu header data
loadNavHeader();
// initializing navigation menu
setUpNavigationView();
if (savedInstanceState == null)
navItemIndex = 0;
CURRENT_TAG = TAG_HOME;
loadHomeFragment();
/***
* Load navigation menu header information
* like background image, profile image
* name, website, notifications action view (dot)
*/
private void loadNavHeader()
// name, website
txtName.setText("Ravi Tamada");
txtWebsite.setText("www.androidhive.info");
// loading header background image
Glide.with(this).load(urlNavHeaderBg)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imgNavHeaderBg);
// Loading profile image
Glide.with(this).load(urlProfileImg)
.crossFade()
.thumbnail(0.5f)
.bitmapTransform(new CircleTransform(this))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imgProfile);
// showing dot next to notifications label
navigationView.getMenu().getItem(3).setActionView(R.layout.menu_dot);
/***
* Returns respected fragment that user
* selected from navigation menu
*/
private void loadHomeFragment()
// selecting appropriate nav menu item
selectNavMenu();
// set toolbar title
setToolbarTitle();
// if user select the current navigation menu again, don't do anything
// just close the navigation drawer
if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null)
drawer.closeDrawers();
// show or hide the fab button
//toggleFab();
return;
// Sometimes, when fragment has huge data, screen seems hanging
// when switching between navigation menus
// So using runnable, the fragment is loaded with cross fade effect
// This effect can be seen in GMail app
Runnable mPendingRunnable = new Runnable()
@Override
public void run()
// update the main content by replacing fragments
Fragment fragment = getHomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out);
fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
fragmentTransaction.commitAllowingStateLoss();
;
// If mPendingRunnable is not null, then add to the message queue
if (mPendingRunnable != null)
mHandler.post(mPendingRunnable);
//Closing drawer on item click
drawer.closeDrawers();
// refresh toolbar menu
invalidateOptionsMenu();
private Fragment getHomeFragment()
switch (navItemIndex)
case 0:
// home
HomeFragment homeFragment = new HomeFragment();
return homeFragment;
case 1:
// photos
PhotosFragment photosFragment = new PhotosFragment();
return photosFragment;
case 2:
// movies fragment
MoviesFragment moviesFragment = new MoviesFragment();
return moviesFragment;
case 3:
// notifications fragment
NotificationsFragment notificationsFragment = new NotificationsFragment();
return notificationsFragment;
case 4:
// settings fragment
SettingsFragment settingsFragment = new SettingsFragment();
return settingsFragment;
default:
return new HomeFragment();
private void setToolbarTitle()
getSupportActionBar().setTitle(activityTitles[navItemIndex]);
private void selectNavMenu()
navigationView.getMenu().getItem(navItemIndex).setChecked(true);
private void setUpNavigationView()
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem)
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId())
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.home:
navItemIndex = 0;
CURRENT_TAG = TAG_HOME;
break;
case R.id.nav_photos:
navItemIndex = 1;
CURRENT_TAG = TAG_PHOTOS;
break;
case R.id.nav_movies:
navItemIndex = 2;
CURRENT_TAG = TAG_MOVIES;
break;
case R.id.nav_notifications:
navItemIndex = 3;
CURRENT_TAG = TAG_NOTIFICATIONS;
break;
case R.id.nav_settings:
navItemIndex = 4;
CURRENT_TAG = TAG_SETTINGS;
break;
case R.id.nav_about_us:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, AboutUsActivity.class));
drawer.closeDrawers();
return true;
default:
navItemIndex = 0;
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked())
menuItem.setChecked(false);
else
menuItem.setChecked(true);
menuItem.setChecked(true);
loadHomeFragment();
return true;
);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
@Override
public void onDrawerClosed(View drawerView)
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
@Override
public void onDrawerOpened(View drawerView)
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
;
//Setting the actionbarToggle to drawer layout
drawer.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessary or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
@Override
public void onBackPressed()
if (drawer.isDrawerOpen(GravityCompat.START))
drawer.closeDrawers();
return;
// This code loads home fragment when back key is pressed
// when user is in other fragment than home
if (shouldLoadHomeFragOnBackPress)
// checking if user is on other navigation menu
// rather than home
if (navItemIndex != 0)
navItemIndex = 0;
CURRENT_TAG = TAG_HOME;
loadHomeFragment();
return;
super.onBackPressed();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
// show menu only when home fragment is selected
if (navItemIndex == 0)
getMenuInflater().inflate(R.menu.main, menu);
// when fragment is notifications, load the menu created for notifications
if (navItemIndex == 3)
getMenuInflater().inflate(R.menu.notifications, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_logout)
Toast.makeText(getApplicationContext(), "Logout user!", Toast.LENGTH_LONG).show();
return true;
// user is in notifications fragment
// and selected 'Mark all as Read'
if (id == R.id.action_mark_all_read)
Toast.makeText(getApplicationContext(), "All notifications marked as read!", Toast.LENGTH_LONG).show();
// user is in notifications fragment
// and selected 'Clear All'
if (id == R.id.action_clear_notifications)
Toast.makeText(getApplicationContext(), "Clear all notifications!", Toast.LENGTH_LONG).show();
return super.onOptionsItemSelected(item);
【问题讨论】:
这是整个文件吗?CoordinatorLayout
没有结束标记。 (</android.support.design.widget.CoordinatorLayout>
)
此链接可能对您有所帮助***.com/questions/30571097/…
确定我有!我有任何语法错误。
【参考方案1】:
我找到了解决方案,它是在你的 styles-v21.xml 中创建以下主题(如果你导入了示例文件,它应该已经存在,但如果它不存在,请创建它):
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
并在清单中,在主要活动部分中调用它,如下所示(删除我之前突出显示的行并将其放在它的位置):
android:theme="@style/AppTheme.NoActionBar">
这应该可以解决问题。
更多解释:https://github.com/Marketcloud/marketcloud-sample-android-application/issues/1
【讨论】:
【参考方案2】:使用 AppCompatActivity 代替 Activity。
【讨论】:
【参考方案3】:请注意,您必须使用 AppCompatActivity(来自 documentation:“使用支持库操作栏功能的 Activity 的基类”。)而不是 Acivity。
【讨论】:
我在主要活动中使用 appcompat,我将分享它。以上是关于InflateException 膨胀类 android.support.design.widget.CoordinatorLayout 时出错的主要内容,如果未能解决你的问题,请参考以下文章
android.view.InflateException:膨胀类 androidx.constraintlayout.widget.ConstraintLayout 时出错
android.view.InflateException:二进制 XML 文件:膨胀类片段时出错
InflateException 膨胀类 android.support.design.widget.CoordinatorLayout 时出错
android.view.InflateException 膨胀类片段时出错,二进制 XML 文件第 1 行?