【问题标题】:You need to use a Theme.AppCompat theme (or descendant) with this activity on Android您需要在 Android 上将此活动与 Theme.AppCompat 主题(或后代)一起使用
【发布时间】:2016-06-24 07:27:21
【问题描述】:

我在活动中有这个:

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"&gt;

我用谷歌搜索,解决方案说将 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>

【问题讨论】:

  • 在这里发布你的styles.xml文件。
  • @Ironman 我添加了,谢谢。

标签: android android-layout android-actionbar


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    改变这个

    <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 它。

    【讨论】:

    • @jason 在下面看到我的回答。
    • @jason 查看我的完整代码,styles.xml 文件中只有此代码。
    【解决方案3】:

    改变这两行

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

    使用这些

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 在你的项目中你应该有一个文件app/src/main/res/values/styles.xml
      • 将带有MyMaterialTheme.Base 的行更改为我的答案中的行。
      猜你喜欢
      • 1970-01-01
      • 2014-03-15
      • 2015-11-27
      • 2016-07-04
      • 2016-05-16
      相关资源
      最近更新 更多