您需要在Android上使用此活动的Theme.AppCompat主题(或后代)

时间:2016-06-24 07:27:21

标签: android android-layout android-actionbar

我在活动中有这个:

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener{

    private Toolbar mToolbar;
    private FragmentDrawer drawerFragment;

    private RecyclerView recyclerView;
    private IsAdapter isAdapter;
    private List<Is> isList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        isList = new ArrayList<>();
        isAdapter = new IsAdapter(this, isList);


        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(isAdapter);

        drawerFragment = (FragmentDrawer)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
        drawerFragment.setDrawerListener(this);

        // display the first navigation drawer view on app launch
        displayView(0);
    }

我正在使用我自己的主题(这是材料),它运行良好,然后我在代码中做了一些更改,现在我得到了这个错误。我怎样才能解决这个问题?提前谢谢。

我的清单文件我有:android:theme="@style/MyMaterialTheme">

我用Google搜索,解决方案称将AppCompatActivity更改为ActivityFragmentActivity。当我这样做时,我会在这些方面得到错误:

setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true); 

这是我的styles.xml:

<resources>

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

    </style>

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

4 个答案:

答案 0 :(得分:2)

您需要做的就是将android:theme =“@ style / Theme.AppCompat.Light”添加到AndroidManifest.xml文件中的应用程序标记。

答案 1 :(得分:1)

如果我理解正确,您已在MyMaterialTheme中定义了一种样式values/styles.xml。 您最基本的主题是否扩展了Theme.AppCompat.{Light,Dark}.NoActionBar之一?例如:

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">

答案 2 :(得分:1)

更改这两行

setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true); 

使用这些

setActionBar(mToolbar);
getActionBar().setDisplayShowHomeEnabled(true); 

答案 3 :(得分:1)

更改此

<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

    </style>

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

到这个

<style name="MyMaterialTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

完整代码:

<resources>   

    <style name="MyMaterialTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

然后clean项目和rebuild它。