【问题标题】:Iappcompat v21: material design ActionBar() InflateException error-inflating-classIappcompat v21:材料设计 ActionBar() InflateException error-inflating-class
【发布时间】:2014-12-16 07:40:00
【问题描述】:

尝试将在 (appcompat v20) 中制作的应用移动到新库 appcompat v21

与:appcompat-v7:20 工作得很好

我这样做了:

ActionBarActivity implements ActionBar.TabListener, ActionBar.OnNavigationListener

还有这个:

android.view.InflateException: Binary XML file line #17: Error inflating class android.support.v7.internal.widget.ActionBarOverlayLayout

Error inflating class android.support.v7.internal.widget.ActionBarView

【问题讨论】:

    标签: android nullpointerexception android-actionbar fragment android-appcompat


    【解决方案1】:

    要使用新的appcompat v21,您必须:

    • 扩展ActionBarActivity 而不是FragmentActivity
    • 使用getSupportActionBar() 而不是getActionBar()
    • 使用继承自 Theme.AppCompat 的主题。(例如 Light 或 NoActionBar)

    编辑:23/04/2015

    对于新的appcompat v22.1,您应该使用新的AppCompatActivity 而不是ActionBarActivity

    此外,ActionBar.TabListener、ActionBar.OnNavigationListener:操作栏导航模式已被弃用内联工具栏操作栏不支持。考虑改用其他常见的导航模式。

    源文档: https://developer.android.com/reference/android/support/v7/app/ActionBar.html#addTab(android.support.v7.app.ActionBar.Tab)

    【讨论】:

    • @user3871754 使用工具栏发布您的布局
    • 抱歉错误信息,请在我的所有源代码中再次检查我的答案异常:androidhive.info/2013/10/…
    • 这也可能帮助您解决这个问题:stackoverflow.com/a/26738677/1304830
    • 现在 ActionBarActivity 在 v22 上已被弃用,您应该改用 AppCompatActivity
    【解决方案2】:

    问题比看起来要深得多。

    我的代码是正确的。该主题中的所有建议都是相关且正确的。

    原来外部库包含旧版本support-v4不支持 MATERIAL DESIGN(appcompat-v7:21),但只支持appcompat-v7:20

    这是ActionBar() InflateException error-inflating-class.的原因

    在所有外部库中更新support-v4 将解决该问题。

    我在其他主题中的 build.gradle:

    multiple dex files define Landroid/support/v4/.

    【讨论】:

      【解决方案3】:

      这是工作代码...copmactv7_api5 使用...其他步骤相同

          import android.os.Bundle;
          import android.support.v4.app.Fragment;
          import android.support.v4.app.FragmentManager;
          import android.support.v4.app.FragmentPagerAdapter;
          import android.support.v4.view.ViewPager;
          import android.support.v7.app.ActionBar;
          import android.support.v7.app.ActionBar.Tab;
          import android.support.v7.app.ActionBarActivity;
      
      public class MainActivity extends ActionBarActivity implements
              ActionBar.TabListener {
      
      AppSectionsPagerAdapter mAppSectionsPagerAdapter;
      ViewPager mViewPager;
      
      @SuppressWarnings("deprecation")
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.admin_main_tab);
      
          // Create the adapter that will return a fragment for each of the three
          // primary sections
          // of the app.
          mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(
                  getSupportFragmentManager());
      
          // Set up the action bar.
          final ActionBar actionBar = getSupportActionBar();
          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
      
          // Set up the ViewPager, attaching the adapter and setting up a listener
          // for when the
          // user swipes between sections.
          mViewPager = (ViewPager) findViewById(R.id.pager);
          mViewPager.setAdapter(mAppSectionsPagerAdapter);
          mViewPager
                  .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                      @Override
                      public void onPageSelected(int position) {
                          // When swiping between different app sections, select
                          // the corresponding tab.
                          // We can also use ActionBar.Tab#select() to do this if
                          // we have a reference to the
                          // Tab.
                          actionBar.setSelectedNavigationItem(position);
                      }
                  });
      
          // For each of the sections in the app, add a tab to the action bar.
          for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
              // Create a tab with text corresponding to the page title defined by
              // the adapter.
              // Also specify this Activity object, which implements the
              // TabListener interface, as the
              // listener for when this tab is selected.
              actionBar.addTab(actionBar.newTab()
                      .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                      .setTabListener(this));
          }
      }
      
      /**
       * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
       * one of the primary sections of the app.
       */
      public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
      
          public AppSectionsPagerAdapter(FragmentManager fm) {
              super(fm);
          }
      
          @Override
          public Fragment getItem(int i) {
              switch (i) {
              case 0:
                  return new AdminSettings();
              default:
                  Fragment fragment = new AdminSettings();
                  return fragment;
              }
          }
      
          @Override
          public int getCount() {
              return 3;
          }
      
          @Override
          public CharSequence getPageTitle(int position) {
              return "Section " + (position + 1);
          }
      }
      
      @Override
      public void onTabReselected(Tab arg0,
              android.support.v4.app.FragmentTransaction arg1) {
          // TODO Auto-generated method stub
      
      }
      
      @Override
      public void onTabSelected(Tab tab,
              android.support.v4.app.FragmentTransaction arg1) {
          mViewPager.setCurrentItem(tab.getPosition());
      
      }
      
          @Override
          public void onTabUnselected(Tab arg0,
                  android.support.v4.app.FragmentTransaction arg1) {
              // TODO Auto-generated method stub
      
          }
      
      }
      

      【讨论】:

      • 您的R.id.pager 中有什么内容? &lt;android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;/android.support.v4.view.ViewPager&gt;
      猜你喜欢
      • 2013-04-25
      • 2016-10-03
      • 2014-10-31
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 2015-08-12
      相关资源
      最近更新 更多