Error: non-static method set (int, int) in MainActivity

0

I was copiling the codes when I got to the end and came across errors in MainActivity , I'm using API 16, does anyone help me?

** code **

 import android.app.AlarmManager;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.app.TaskStackBuilder;
 import android.content.Context;
 import android.content.Intent;
 import android.icu.util.Calendar;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.support.v7.app.NotificationCompat;
 import android.view.View;

 public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener(){
        @Override
        public void  onClick(View view){

            Calendar calendar = Calendar.getInstance();

            Calendar.set(Calendar.HOUR_OF_DAY,18);
            Calendar.set(Calendar.MINUTE,45);
            Calendar.set(Calendar.SECOND,00);


            Intent intent = new Intent(getApplicationContext(),Notification_reciever.class);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
        }
    });
   }
 }
    
asked by anonymous 09.09.2017 / 06:57

1 answer

1

The problem starts with import that is not correct. At the top Calendar is being imported with:

import android.icu.util.Calendar;

When the code that is being used from Calendar refers to the standard library of java in java.util . So the import above should be replaced by:

import java.util.Calendar;

In addition the initialization / use of Calendar :

Calendar calendar = Calendar.getInstance();

Calendar.set(Calendar.HOUR_OF_DAY,18);
Calendar.set(Calendar.MINUTE,45);
Calendar.set(Calendar.SECOND,00);

It is not correct either. Notice that the Calendar.set is being done with uppercase, so it refers to the Calendar class and not the calendar object created above that was what was intended.

Change this block of code to:

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.HOUR_OF_DAY,18);
calendar.set(Calendar.MINUTE,45);
calendar.set(Calendar.SECOND,00);
    
09.09.2017 / 11:27