openOptionsMenu()不起作用

时间:2014-04-21 11:54:28

标签: android options-menu

嗯,尝试了很多方法来解决这个问题,但没有...我想通过菜单项打开选项菜单。

    package com.example.tachles;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ShareActionProvider;


public class MainActivity extends AnimActivity {

    private ShareActionProvider mShareActionProvider;
    ImageView history, contact, info, share;

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

        history = (ImageView) findViewById(R.id.imageView2);
        history.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent history = new Intent(getApplicationContext(),
                        HistoryMenu.class);
                startActivity(history);
            }
        });

        contact = (ImageView) findViewById(R.id.imageView8);
        contact.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                String recepientEmail = "mirnax.info@gmail.com";
                Intent intent = new Intent(Intent.ACTION_SENDTO);
                intent.setData(Uri.parse("mailto:" + recepientEmail));

                startActivity(intent);

            }
        });

        info = (ImageView) findViewById(R.id.imageView9);
        info.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent info = new Intent(getApplicationContext(), Odot.class);
                startActivity(info);

            }
        });
    }

    // Menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.actionbar, menu);

        // Get the ActionProvider for later usage
        mShareActionProvider = (ShareActionProvider) menu.findItem(
                R.id.action_share).getActionProvider();

        /** Setting a share intent */
        mShareActionProvider.setShareIntent(getDefaultShareIntent());

        return super.onCreateOptionsMenu(menu);
    }

    private Intent getDefaultShareIntent() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT,
                "תכל'ס - האפליקציה שתלווה אותך לבגרויות");
        intent.putExtra(Intent.EXTRA_TEXT,
                "https://play.google.com/store/apps/details?id=com.tachles");
        return intent;
    }


    public void Options(MenuItem item){

        MainActivity.this.openOptionsMenu();
        }

    public void like(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_like:
            Uri uri = Uri.parse("https://m.facebook.com/tachles.mirnax?_rdr");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }

    }



}

也试过这种方式,它什么也没做...... Android Option Menu on Button click

请帮助......没有错误,没有logcat ......

1 个答案:

答案 0 :(得分:2)

使用以下重写方法创建上下文菜单视图。

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, 1, 0, "Menu 1");
        menu.add(0, 2, 0, "Menu 2");
        menu.add(0, 3, 0, "Menu 3");
    }

使用以下重写方法处理活动代码中的点击侦听器。

    @Override
    public boolean onContextItemSelected(MenuItem item)
    {

        switch (item.getItemId())
        {
        case 1:
            //do your code here
            break;
        case 2:
            //do your code here
            break;
        case 3:
            //do your code here
            break;
        default : break;
            }

onCreateView()中致电registerForContextMenu(yourButton);并实施onClickListener()

YourClassName.this.openContextMenu(v);