Android 工具栏后退箭头单击侦听器不工作

Android 工具栏后退箭头单击侦听器不工作,android,Android,我在使用setSupportActionBar(工具栏)时遇到使用问题我的应用程序因在logcat中说错误而崩溃 Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to fa

我在使用
setSupportActionBar(工具栏)时遇到使用问题我的应用程序因在logcat中说错误而崩溃

 Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
 at android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar
当我删除代码集SupportActionBar(toolsbar)时,这是非常可读的。我的应用程序中下面的代码集不起作用(这是用于导航回上一个活动的工具栏中的后退箭头)



尝试此操作以处理工具栏后退箭头的单击事件

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {
        Toast.makeText(context, "Backarrow pressed", Toast.LENGTH_SHORT).show();
        return true;
    }

    return false;
}
将此主题添加到您的活动中

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

    <item name="colorPrimary">@color/colorWhite</item>
    <item name="colorPrimaryDark">@color/colorWhite</item>
    <item name="colorAccent">@color/colorAccent</item>

</style>

@彩色/彩色白色
@彩色/彩色白色
@颜色/颜色重音

要使用
工具栏
,必须在
样式中设置主题
“theme.AppCompat.Light.NoActionBar”

你的
风格
结构应该是:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

@颜色/原色
@颜色/原色暗
@颜色/颜色重音

使用我的代码及其工作代码:

附加模块Menifest

  <activity
        android:name=".DemoActivity"
        android:theme="@style/AppTheme.NoActionBar" />
对于BackClick事件:(放在
onCreate()之外

试试这个

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == android.R.id.home){
        Toast.makeText(context, "Backarrow pressed", Toast.LENGTH_SHORT).show();
        //finish();
    }
    return super.onOptionsItemSelected(item);
}
最好的解决方案是,您必须使用一个全局活动,该活动包括您的工具栏布局、导航以及您希望在整个应用程序中使用的所有控件,然后您只需扩展该活动,而不是像下面的示例所示的活动:
公共类YourCurrentActivity扩展了实用性{
......
}
然后,您必须按照以下步骤创建应用程序主题:
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
现在,您只需在清单文件中设置当前活动的父活动,如下所示:

尝试在清单活动标记android:theme=“@style/AppTheme.NoActionBar”中添加此项,如果我使用的是工具栏,则尝试使用NoActionBar主题。setNavigationOnClickListener(new View.OnClickListener(){@Override public void onClick(View v){Toast.makeText(上下文,“按反箭头键”),Toast.LENGTH_SHORT).show();}});这部分代码不起作用。您也可以尝试使用自定义按钮创建自定义工具栏,并处理该按钮上的单击事件。我不知道@AbhijitChakra如果有帮助,您可以接受ans
  Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
        TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
        mTitle.setText("Open Docket");
        setSupportActionBar(toolbarTop);

        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setDisplayShowTitleEnabled(false);
        ab.setHomeAsUpIndicator(R.mipmap.ic_back_appbar);
  @Override
    public boolean onSupportNavigateUp(){
        finish();
        return true;
    }
 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == android.R.id.home){
        Toast.makeText(context, "Backarrow pressed", Toast.LENGTH_SHORT).show();
        //finish();
    }
    return super.onOptionsItemSelected(item);
}
The best solution is you have to use a Global activity that consist your toolbar layout, navigation and all control that you want to use in your whole application, then you have to just extend that activity instead of Activity like below example:

public class YourCurrentActivity extends DrawerActivity {
......
}

then you have to create application theme as per below:
<resources>
       <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

now, you have to just set the parent activity of your current activity in manifest file  like below:

  <activity
            android:name=".YourCurrentActivity"
            android:parentActivityName=".ParentActivityName"
            android:screenOrientation="portrait" />