操作栏菜单项显示但不起作用

时间:2016-05-12 09:07:17

标签: android

操作栏上的菜单图标无效。图标正在显示但不可点击。我也应用不同的主题,但没有帮助。

风格

<resources>

    <!-- Base application theme. -->
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.ActionBar" />
</resources>

我的主要活动

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private TextView time,fajar,zahr,asar,magrib,isha,jumma;
    private TextView namaz,hadith,khatam_e_nubuwat,setting;
    DrawerLayout drawerLayout;
    Toolbar toolbar;
    private ActionBarDrawerToggle actionBarDrawerToggle;

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

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

        getSupportActionBar().setDisplayShowHomeEnabled(true);/// this line giving null point exception..
        getSupportActionBar().setDisplayUseLogoEnabled(true);


        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        FindElement();
        SetClick();
        drawerIcon();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_main, menu);



        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();


        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Log.d("111111111", "onOptionsItemSelected: "); //// this is the menu item which is not working also drawer icon is not working....
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

android Manifest

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.Holo.Light.DarkActionBar" >
        <activity
            android:name=".SliderViewPager"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <!--android:noHistory="true">-->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"
            android:theme="@style/AppTheme"/>
    </application>

这是我的menu.xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.amir.namaztimer.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="ifRoom"
        android:enabled="true"
        />

</menu>

我更新了我的代码并包含menu.xml

2 个答案:

答案 0 :(得分:0)

   @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {

        int itemId = item.getItemId();
        switch (itemId) {
        case android.R.id.home:
    Toast.makeText(this, "You Pressed home button",Toast.LENGTH_LONG).show();
            break;}
        return true;
    }

答案 1 :(得分:0)

我设法找到问题并解决了它。问题在于抽屉布局在content.xml文件中我带这个抽屉并将其放在Activity.xml中并解决问题....

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/content_main" />

     </FrameLayout>