Reputation: 2793
My pop up menu displays with these options:
New Contact
Share
Settings
When 'New Contact' is clicked it should open the activity Make_a_contact. But nothing happens - the pop up menu just closes down, like nothing is supposed to happen. Any ideas? They'd be much appreciated. The code I have at present is:
In my Manifest:
<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>
In my 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();
}
}
My menu looks like:
<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);
}
}
Edit Note: Thanks to Greg below I now know the problem is with setOnMenuItemClickListener
But how do I implement this to get my popupmenu to work? I am obviously missing code somewhere. In thisisatest.java I have modified the public void Show_Settings
nethod so it now reads:
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();
}
When I build the project I get the error:
Error:(91, 18) error: non-static method setOnMenuItemClickListener(OnMenuItemClickListener) cannot be referenced from a static context
Upvotes: 0
Views: 659
Reputation: 15379
You need to set a click listener on the popup menu using setOnMenuItemClickListener
and handle the click there.
The onCreateOptionsMenu
/onOptionsMenuSelected
calls are used for showing action bar items in your activity toolbar, completely different thing from your popup menu.
Upvotes: 2