Android Studio - PopupMenu.OnMenuItemClickListener问题

问题描述 投票:3回答:4

我正在编写一个移动应用程序,并通过该按钮的OnClick方法将一个弹出菜单附加到ImageButton。

    menu_lilac.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            showPopup(view);

        }
    });

我试图让它按下菜单项重定向到相关的Activity(在最后的switch语句中)。我得到的错误是错误:(23,8)错误:MainActivity不是抽象的,并且不会覆盖OnMenuItemClickListener中的抽象方法onMenuItemClick(MenuItem)。

public void showPopup(View view) {
    PopupMenu popup = new PopupMenu(this, view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.mainmenu, popup.getMenu());
    MenuItem item = (MenuItem) popup.getMenu();
    popup.show();

    //Intent to Tasks Activity
    final Intent toTasks = new Intent(this, TasksActivity.class);

    //Intent to Distractions Activity
    final Intent toDist = new Intent(this, DistractionsActivity.class);

    //Intent to Settings Activity
    final Intent toSett = new Intent(this, SettingsActivity.class);

    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.Home:
                    return true;

                case R.id.Tasks:
                    startActivity(toTasks);
                    return true;

                case R.id.Distractions:
                    startActivity(toDist);
                    return true;

                case R.id.Settings:
                    startActivity(toSett);
                    return true;

                default:
                    return false;
            }

    }

    });
}

我究竟做错了什么?我尝试以不同的方式初始化OnMenuItemClickListener,例如

    PopupMenu.OnMenuItemClickListener listener = PopupMenu.OnMenuItemClickListener(){


    }

但是从不使用监听器,当按下任何菜单按钮时应用程序崩溃。

完整的MainActivity类:

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.NumberPicker;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ListView;

public class MainActivity extends Activity implements PopupMenu.OnMenuItemClickListener {

//Creation of used objects
//Button, Button, TextView, Handler, AlarmManager, NumberPicker, Pending Intent
//Context, TextView, Button

Button startButton;
Button stopButton;
TextView timerValue;
Handler customHandler = new Handler();
NumberPicker interval_length;
PendingIntent pending_intent;
Context context;
TextView checker;
ImageButton menu_lilac;
Intent toDist;
Intent toSett;
Intent toTasks;

//Creation of variables
long timeInMilliseconds = 0L;
long updatedTime = 0L;
long startTime = 0L;
int picked_value;
private static int minValueInterval = 1;
private static int maxValueInterval = 60;
int final_interval;

//On create function initialises the layout
@Override
protected void onCreate(Bundle savedInstanceState) {
    //Creation of main GUI layout
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.context = this;
}


@Override
protected void onStart() {
    super.onStart();

    //Initialise number picker
    interval_length = (NumberPicker) findViewById(R.id.interval_length);

    //Set minimum value (minutes)
    interval_length.setMinValue(minValueInterval);
    //Set maximum value (minutes)
    interval_length.setMaxValue(maxValueInterval);
    //Set currently displayed value (minutes)
    interval_length.setValue(20);

    //Initialise listener to listen for a value change on the number picker
    interval_length.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            //Replace variable picked_value with the new value of the number picker
        picked_value = interval_length.getValue() - 1;
        }
    });

    //Initialise timerValue text box
    timerValue = (TextView) findViewById(R.id.timerValue);

    //Initialise start button
    startButton = (Button) findViewById(R.id.startButton);

    //Initialise instance of stop button
    stopButton = (Button) findViewById(R.id.stopButton);

    //Initialise instance of menu button
    menu_lilac = (ImageButton) findViewById(R.id.menu_lilac);

    //Initialise checker text box
    checker = (TextView) findViewById(R.id.checker);

    //Menu button onClickListener
    menu_lilac.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            showPopup(view);

        }
    });

            //Start button OnClickListener
    startButton.setOnClickListener(new View.OnClickListener() {
        //On start begin counting up in milliseconds.
        //Reset timer every time Start button is pressed.
        public void onClick(View view) {
            timerValue.setText("00:00");
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);

            //Set text box to print a message displaying when the alarm is for
            checker.setText("The alarm has been set for " + picked_value + " minutes from now!");

            //Make pop-up toast notification
            Toast.makeText(context,"Alarm on!", Toast.LENGTH_LONG).show();
        }
    });

    //Calculation of picked interval length (from minutes) to milliseconds
    final_interval = picked_value * 60 * 1000;
    if (timerValue.equals(picked_value+":00")) {
        alarm_manager.set(AlarmManager.RTC_WAKEUP, timeInMilliseconds, pending_intent);
    }

    //Initialise Stop button OnClickListener
    stopButton.setOnClickListener(new View.OnClickListener() {
        //On click stop updating timer thread and reset value to 00:00
        public void onClick(View view) {
            customHandler.removeCallbacks(updateTimerThread);
            timerValue.setText("00:00");

            //print message to notify user of alarm being cancelled
            checker.setText("The alarm has been cancelled!");
            //Make toast notification to say alarm cancelled
            Toast.makeText(context,"Alarm off!", Toast.LENGTH_LONG).show();
        }
    });
}
//Creates a runnable to update the timer
private Runnable updateTimerThread = new Runnable() {
    public void run() {
        //Takes time in milliseconds from the system clock
        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
        updatedTime = timeInMilliseconds;
        //Converts milliseconds to seconds and minutes
        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        //Updates timerValue to the formatted 00:00 (minutes:seconds)
        timerValue.setText(String.format("%02d", mins) + ":"
                + String.format("%02d", secs));
        customHandler.postDelayed(this, 0);
    }
};

public Runnable showPopup(View view) {
    PopupMenu popup = new PopupMenu(this, view);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.mainmenu, popup.getMenu());
    MenuItem item = (MenuItem) popup.getMenu();
    popup.show();

    //Intent to Tasks Activity
    final Intent toTasks = new Intent(this, TasksActivity.class);

    //Intent to Distractions Activity
    final Intent toDist = new Intent(this, DistractionsActivity.class);

    //Intent to Settings Activity
    final Intent toSett = new Intent(this, SettingsActivity.class);

    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.Home:
                    return true;

                case R.id.Tasks:
                    startActivity(toTasks);
                    return true;

                case R.id.Distractions:
                    startActivity(toDist);
                    return true;

                case R.id.Settings:
                    startActivity(toSett);
                    return true;

                default:
                    return false;
            }

    }

    });
}

}

java android popupmenu
4个回答
1
投票

我不明白为什么你实际在你的活动中实现接口PopupMenu.OnMenuItemClickListener,但如果有理由,你必须实现该接口的方法。

所以基本上你在MainActivity中使用该方法的must have implementation

@Override
public boolean onMenuItemClick(MenuItem item) {
    return false;
}

也希望您使用Android Studio。它应该说明你的错误,如果你将指针放在错误上并单击“ALT + ENTER”,你会看到菜单中的实现方法项,点击它将解决该问题。

enter image description here


1
投票

固定!我从我的类声明中删除了“implements PopupMenu.OnMenuItemClickListener”,并重新完成了PopupMenu。这次我使用if语句而不是switch语句而不是调用MenuInflater我刚刚在弹出窗口中使用了inflate()。

这些是所需的进口:

import android.view.MenuItem;
import android.widget.PopupMenu;

我的班级声明:

public class MainActivity extends Activity {

这些是我的意图:

    //Intent to Tasks Activity
    final Intent toTasks = new Intent(this, TasksActivity.class);

    //Intent to Distractions Activity
    final Intent toDist = new Intent(this, DistractionsActivity.class);

    //Intent to Settings Activity
    final Intent toSett = new Intent(this, SettingsActivity.class);

这是我的弹出菜单。

   //Menu button onClickListener
    menu_lilac.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            //create instance of PopupMenu
            PopupMenu popup = new PopupMenu(getApplicationContext(), view);
            //inflate menu with layout mainmenu
            popup.inflate(R.menu.mainmenu);
            popup.show();

            //Set on click listener for the menu
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    if (item.getItemId()== R.id.Home){
                        Toast.makeText(context, "At home!", Toast.LENGTH_LONG).show();
                    }
                    if (item.getItemId() == R.id.Tasks){
                        startActivity(toTasks);
                    }
                    if (item.getItemId() == R.id.Distractions){
                        startActivity(toDist);
                    }
                    if (item.getItemId() == R.id.Settings){
                        startActivity(toSett);
                    }
                    return false;
                }
            });
        }
    });

0
投票

对于迟到的访客可能会有帮助

public class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {

ImageButton mainMenu;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mainMenu = findViewById(R.id.ibtn_menu);

    mainMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showMenu(view);
        }
    });
}

 public void showMenu(View v) {
    PopupMenu popup = new PopupMenu(this, v);

    // This activity implements OnMenuItemClickListener
    popup.setOnMenuItemClickListener(this);
    popup.inflate(R.menu.main_menue);
    popup.show();
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_day_view_about: {
            Toast.makeText(MainActivity.this,"Clicked on the action", Toast.LENGTH_LONG).show();
            return true;
        }
    }
    return true;
}
}

0
投票

我的问题是我实现了错误的OnMenuItemClickListener,

我需要:

PopupMenu.OnMenuItemClickListener

我有:

MenuItem.OnMenuItemClickListener
© www.soinside.com 2019 - 2024. All rights reserved.