2017-03-06 86 views
3

我正在編寫一個移動應用程序,並通過該按鈕的OnClick方法將一個彈出菜單附加到ImageButton。Android Studio - PopupMenu.OnMenuItemClickListener問題

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

     } 
    }); 

我想讓它按下菜單項重定向到相關的活動(在最後的switch語句中)。 我得到的錯誤是錯誤:(23,8)錯誤:MainActivity不是抽象的,並且不會覆蓋OnMenuItemClick(MenuItem)onMenuItemClickListener中的抽象方法。

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; 
      } 

    } 

    }); 
} 

}

+0

顯示'MainActivity'的代碼,如果它擴展了任何其他類,除了標準的android之外,也可以顯示它。 – Divers

+0

@Divers顯示的代碼在MainActivity中。它像這樣開始「公共類MainActivity擴展Activity實現PopupMenu.OnMenuItemClickListener {」... – Kate

+0

顯示MainActivity的所有代碼。包括進口。 – Divers

回答

0

固定!我從我的類聲明中刪除了「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; 
       } 
      }); 
     } 
    }); 
1

我不明白爲什麼你真正實現接口PopupMenu.OnMenuItemClickListener在您的活動,但是,如果有理由,你必須實現接口的方法。

所以基本上你說的方法must have implementationMainActivity

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

也希望你使用Android工作室。它應該說你有關錯誤,如果你將指針放在錯誤,並點擊「ALT + ENTER」,你會看到在菜單中執行方法項目,點擊哪個將解決這個問題。

enter image description here

+0

我應該將switch語句移到這個實現的方法中,而不是在我的onMenuItemClick方法中? – Kate

+1

'popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener ...)' - 與在活動中執行'popup.setOnMenuItemClickListener(this)'並實現'PopupMenu.OnMenuItemClickListener'相同。你可以做到這一點。你需要花更多的時間來理解Java語法。 – Divers