Iappcompat v21:材料设计ActionBar()InflateException error-inflating-class

时间:2014-10-21 06:54:58

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

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

with: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

3 个答案:

答案 0 :(得分:11)

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

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

编辑:2015年4月23日

使用新的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)

答案 1 :(得分:6)

问题比看上去要深刻得多。

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

事实证明,外部库包含旧版本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/

答案 2 :(得分:4)

这是工作代码... 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

    }

}