弹出菜单未启动新 activity

popup menu not launching new activity

我的弹出菜单显示了这些选项:

New Contact
Share 
Settings

单击 'New Contact' 时,它应该会打开 activity Make_a_contact。但什么也没有发生——弹出菜单就这样关闭了,就像什么都不应该发生一样。有任何想法吗?他们将不胜感激。我目前的代码是:

在我的清单中:

<uses-sdk
    android:minSdkVersion="11"
    />


<uses-permission android:name="android.permission.READ_CONTACTS"/>



<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <activity
        android:name=".thisisatest"
        android:label="@string/title_activity_thisisatest">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Make_a_contact"
        android:label="@string/title_activity_make_a_contact" >
    </activity>
</application>

在我的thisisatest.java:

    package com.example.chris.omgandroid;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.SimpleCursorAdapter;


public class thisisatest extends ListActivity {
    @Override
    public long getSelectedItemId() {
        return super.getSelectedItemId();
    }
@Override
public int getSelectedItemPosition() {
    return super.getSelectedItemPosition();
}

ListView lv;
Cursor cursorl;


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_thisisatest, 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) {
        return true;
    }

    //noinspection SimplifiableIfStatement
    if (id == R.id.New_Contact) {

        Intent intent = new Intent(this, Make_a_contact.class);
        startActivity(intent);

        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void Show_Settings(View v){

    PopupMenu popup = new PopupMenu(this, v );
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_thisisatest, popup.getMenu());
    popup.show();
}
}

我的菜单如下所示:

   <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.chris.omgandroid.thisisatest">

<item
    android:id = "@+id/New_Contact"
    android:title = "New Contact"
/>

    <item
        android:id = "@+id/Share"
        android:title = "Share"
    />

    <item
        android:id = "@+id/Settings"
        android:title = "Settings"
    />
</menu>

Make_a_contact class:

package com.example.chris.omgandroid;

import android.app.Activity; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem;


public class Make_a_contact extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_make_a_contact, 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) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

编辑注意: 感谢下面的 Greg 我现在知道问题出在 setOnMenuItemClickListener 但是我如何实现它才能让我的弹出菜单工作?我显然在某处缺少代码。在 thisisatest.java 中,我修改了 public void Show_Settings 方法,现在它显示为:

   public void Show_Settings(View v){

        PopupMenu popup = new PopupMenu(this, v );
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.menu_thisisatest, popup.getMenu());

        PopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(getApplicationContext(),item.toString(),Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        popup.show();
    }

当我构建项目时出现错误:

Error:(91, 18) error: non-static method setOnMenuItemClickListener(OnMenuItemClickListener) cannot be referenced from a static context

您需要使用 setOnMenuItemClickListener 在弹出菜单上设置点击侦听器并在那里处理点击。

onCreateOptionsMenu/onOptionsMenuSelected 调用用于在 activity 工具栏中显示操作栏项目,与弹出菜单完全不同。