Mainactivity error :non-static method set(int,int)

Asked

Viewed 26 times

0

I was running the codes when I got to the end and came across errors in MainActivity, I’m using API 16, someone help me ?

erro na Main Activity

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

1 answer

1


The problem begins in the import that is not correct. On top the Calendar is being imported with :

import android.icu.util.Calendar;

When the code being used of Calendar refers to the library standard of java in java.util. Soon the import quoted above should be exchanged for:

import java.util.Calendar;

In addition to the initialisation/use of Calendar:

Calendar calendar = Calendar.getInstance();

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

Not correct either. Note that the Calendar.set is being done with capital initial, therefore refers to the class Calendar and not the object calendar created above what was intended.

Exchange this code block for:

Calendar calendar = Calendar.getInstance();

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.