为什么不调用所选上下文项的函数

时间:2015-10-19 08:53:37

标签: java android fragment contextmenu

我正在使用标签布局。我已经在我的片段类中定义了一些函数,但是在上下文选择方法的函数调用中,它给出了错误"没有创建方法。" 我也在onACtivity方法中编写代码但是没有用。 请帮忙!

Java代码

public class Tab2 extends Fragment 
{
    TimePicker myTimePicker;
    Button buttonstartSetDialog;
    Button buttonstartEndDialog;
    Button buttonCancelAlarm;
    TextView textAlarmPrompt;
    TextView textAlarmPrompt2;


    TimePickerDialog timePickerDialog;

    final static int RQS_1 = 1;
    final static int RQS_2 = 2;
     int FIRST_OPTION = 1;
     int SECOND_OPTION = 2;
     int Third_OPTION = 3;
     int Fourth_OPTION = 4;

    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.set_time,container,false);
        return v;}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);


    textAlarmPrompt = (TextView)getView().findViewById(R.id.alarmprompt);
    textAlarmPrompt2 = (TextView)getView().findViewById(R.id.alarmprompt2);

    buttonstartSetDialog = (Button)getView().findViewById(R.id.startSetDialog);
    buttonstartSetDialog.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            textAlarmPrompt.setText("");
            openTimePickerDialog(false);
            //buttonstartSetDialog.setClickable(false);
            //buttonstartSetDialog.setClickable(false);
            //buttonCancelAlarm.setClickable(true);

        }


            private void openTimePickerDialog(boolean is24r){
                Calendar calendar = Calendar.getInstance();

                timePickerDialog = new TimePickerDialog(
                        getActivity(), 
                        onTimeSetListener, 
                        calendar.get(Calendar.HOUR_OF_DAY), 
                        calendar.get(Calendar.MINUTE), 
                        is24r);

                timePickerDialog.setTitle("Set Alarm Time");  

                timePickerDialog.show();
            }

            OnTimeSetListener onTimeSetListener
            = new OnTimeSetListener(){

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                    Calendar calNow = Calendar.getInstance();
                    Calendar calSet = (Calendar) calNow.clone();

                    calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    calSet.set(Calendar.MINUTE, minute);
                    calSet.set(Calendar.SECOND, 0);
                    calSet.set(Calendar.MILLISECOND, 0);

                    if(calSet.compareTo(calNow) <= 0){
                        //Today Set time passed, count to tomorrow
                        calSet.add(Calendar.DATE, 1);
                    }

                    setAlarm(calSet);

                }};

            private void setAlarm(Calendar targetCal){


                textAlarmPrompt.setText(
                        "\n\n***\n"
                        + "Alarm is set@ " + targetCal.getTime() + "\n"
                        + "***\n");

                Intent intent = new Intent(getActivity(), AlarmReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), RQS_1, intent, 0);
                AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
                alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
            }

            private void cancelAlarm(){


                textAlarmPrompt.setText(
                        "\n\n***\n"
                        + "Alarm Cancelled! \n"
                        + "***\n");

                Intent intent = new Intent(getActivity(), AlarmReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), RQS_1, intent, 0);
                AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
                alarmManager.cancel(pendingIntent);

            }});

    //Registering Start Time Button for context menu

    registerForContextMenu(buttonstartSetDialog);
    buttonstartSetDialog.setOnCreateContextMenuListener(new OnCreateContextMenuListener() 
    {

        @Override
        public void onCreateContextMenu(ContextMenu menu, View arg1,
                ContextMenuInfo arg2) {
            menu.setHeaderTitle("Choose");
            menu.add(0, FIRST_OPTION, 0, "Edit");
            menu.add(0, SECOND_OPTION, 0, "Delete");

        }
    });



    /********* Opening Time picker dialog from end time button *********/

    buttonstartEndDialog = (Button)getView().findViewById(R.id.startEndDialog);

    buttonstartEndDialog.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {


            textAlarmPrompt2.setText("");
            openTimePickerDialog2(false);
            //buttonstartSetDialog.setClickable(false);
            //buttonstartEndDialog.setClickable(false);
            //buttonCancelAlarm.setClickable(true);

        }

            private void openTimePickerDialog2(boolean is24r){
                Calendar calendar = Calendar.getInstance();

                timePickerDialog = new TimePickerDialog(
                        getActivity(), 
                        onTimeSetListener2, 
                        calendar.get(Calendar.HOUR_OF_DAY), 
                        calendar.get(Calendar.MINUTE), 
                        is24r);

                timePickerDialog.setTitle("Set Alarm Time");  

                timePickerDialog.show();
            }

            OnTimeSetListener onTimeSetListener2
            = new OnTimeSetListener(){

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                    Calendar calNow = Calendar.getInstance();
                    Calendar calSet = (Calendar) calNow.clone();

                    calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    calSet.set(Calendar.MINUTE, minute);
                    calSet.set(Calendar.SECOND, 0);
                    calSet.set(Calendar.MILLISECOND, 0);

                    if(calSet.compareTo(calNow) <= 0){
                        //Today Set time passed, count to tomorrow
                        calSet.add(Calendar.DATE, 1);
                    }

                    setAlarm2(calSet);

                }};

            private void setAlarm2(Calendar targetCal){

                textAlarmPrompt2.setText(
                        "\n\n***\n"
                        + "Alarm is set@ " + targetCal.getTime() + "\n"
                        + "***\n");

                Intent intent2 = new Intent(getActivity(), AlarmReciever_for_Normal.class);
                PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getActivity(), RQS_2, intent2, 0);
                AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
                alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent2);
        //buttonstartSetDialog.setClickable(false);
                //buttonCancelAlarm.setEnabled(false);
            }
            private void cancelAlarm2(){

                textAlarmPrompt2.setText(
                        "\n\n***\n"
                        + "Alarm Cancelled! \n"
                        + "***\n");

                Intent intent2 = new Intent(getActivity(), AlarmReciever_for_Normal.class);
                PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getActivity(), RQS_2, intent2, 0);
                AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
                alarmManager.cancel(pendingIntent2);

            }});

    registerForContextMenu(buttonstartEndDialog);
    buttonstartEndDialog.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

            @Override
            public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenuInfo menuInfo) {
                menu.setHeaderTitle("Choose");
                menu.add(0, Third_OPTION, 0, "Edit");
                menu.add(0, Fourth_OPTION, 0, "Delete");


            }});


}
 @Override
    public boolean onContextItemSelected(MenuItem item) {

        int itemId = item.getItemId();
        if(itemId == FIRST_OPTION){
        openTimePickerDialog(false);    
        return true;

        }
        else if(itemId == SECOND_OPTION){

            cancelAlarm();
            return true;

        }
        else if(itemId == Third_OPTION){
            openTimePickerDialog2(false);   
            return true;

            }
            else if(itemId == Fourth_OPTION){

                cancelAlarm2();
                return true;

            }
        return super.onContextItemSelected(item);

    }



}

1 个答案:

答案 0 :(得分:1)

编辑 cancelAlarm()定义在错误的位置,因为它位于OnClickListener内,而其他方法无法看到它。

考虑将其与其他方法放在同一水平。另外,通过这种方式,您无法访问onActivityCreated中定义的某些变量,您应该为它们创建字段变量。

public class Tab2 extends Fragment 
{
    ...

   @Override
   public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {...}

   @Override
   public void onActivityCreated(Bundle savedInstanceState) {}

   @Override
   public void onCreateContextMenu(ContextMenu menu, View arg1, ContextMenuInfo arg2) {});

   private void openTimePickerDialog(boolean is24r){...}

   OnTimeSetListener onTimeSetListener = new OnTimeSetListener(){...};

   private void setAlarm(Calendar targetCal){...}

   private void cancelAlarm(){...}
}