我的弹出菜单显示以下选项:
New Contact
Share
Settings
当'新联系人'单击它应该打开活动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
但是如何实现这个以使我的popupmenu工作?我显然在某处遗漏了代码。在thisisatest.java中,我修改了public void Show_Settings
nethod,现在它为:
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
答案 0 :(得分:2)
您需要使用printArr3(arr3);
在弹出菜单上设置点击监听器并处理其中的点击。
setOnMenuItemClickListener
/ onCreateOptionsMenu
次调用用于显示活动工具栏中的操作栏项,与弹出式菜单完全不同。