خرید بک لینک

Vote count: 0

i have a main activity to set alarm and create notification :

public class MainActivity extends Activity {


@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable 
PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);

}

public void scheduleNotification(Notification notification, long delay) {

    Intent notificationIntent = new Intent(this, MyBroadcastReceiver.class);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, 
   notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, 
   notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    long futureInMillis = delay;
    AlarmManager alarmManager =
(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, futureInMillis, 
    pendingIntent);
}

public Notification getNotification(String content) { //ERROR IS HERE Line 42
    Notification.Builder builder = new Notification.Builder(this);//OR HERE
    builder.setContentTitle("Scheduled Notification");
    builder.setContentText(content);
    builder.setSmallIcon(R.drawable.ic_audiotrack_light);
    return builder.build();
}
}`

and i try to call in My Service

public class DateForAlarmService extends Service{
    Date date1;
    Realm realm;
    DateStorageModel dsm;
    getCurrentDateTime gtm;
    String TAG = "";
    long millisJamJK;
    MainActivity MA;

public DateForAlarmService(){
    gtm = new getCurrentDateTime();
    MA = new MainActivity();
}

@Override
public void onCreate() {
    super.onCreate();

}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}



public void getJadwalKuliahEarly(TextView txt1){
    MA = new MainActivity();
    realm = Realm.getDefaultInstance();
    int noHari = getNoHariHariIni(gtm.getEEE());
    final int year0 = 2011;
    final int month0 = 1;
    final int day0 = 1;
    Calendar c = Calendar.getInstance();
    int jam = c.get(Calendar.HOUR_OF_DAY);
    int minutes = c.get(Calendar.MINUTE);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    String formatedDate = sdf.format(new 
Date(year0,month0,day0,jam,minutes));
    try {
        Date dateMowForJK = sdf.parse(formatedDate);
        JadwalKuliahModel jkm = realm.where(JadwalKuliahModel.class).equalTo("nohari",noHari).greaterThan("waktu_jk",dateMowForJK).findFirst();
        if (jkm !=null) {
            SimpleDateFormat formatjam = new SimpleDateFormat("HH:mm");
            Date now = new Date();
            String Nows = formatjam.format(now);
            String JK = formatjam.format(jkm.getWaktu_jk());

            long Now = (formatjam.parse(JK).getTime()) -  (formatjam.parse(Nows).getTime());
            MA.scheduleNotification(MA.getNotification(jkm.getMakul_jk()),Now);
            txt1.setText(jkm.getMakul_jk());



        }else{
            txt1.setText("Tidak Ada Jadwal");
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }

And this is my BroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";

@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = intent.getParcelableExtra(NOTIFICATION);
    int id = intent.getIntExtra(NOTIFICATION_ID, 0);
    notificationManager.notify(id,notification);

}
}

in manifest i have write my Service, my BroadcastReceiver, and myMainActivity.

But i have error when the Service input time in millis for my MainActivity. the error is :

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
                                                 at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:150)
                                                 at android.app.Notification$Builder.(Notification.java:2181)
                                                 at me.citrafa.asistenkuliahku.MainActivity.getNotification(MainActivity.java:42)
                                                 at me.citrafa.asistenkuliahku.DateForAlarmService.getJadwalKuliahEarly(DateForAlarmService.java:87)
                                                 at me.citrafa.asistenkuliahku.ActivityClass.Dashboard.onCreate(Dashboard.java:137)

the error is in MainActivity, i have change Context on Notification.Builder(HERE), to MainActivity.this, this, getApplication, getApplicationContext, and etc, but still error.

the reason why i call that notification in service is, when notification is notified, i want to trigger service for find next data, for next notification.

How to fix this?

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 10 ارديبهشت 1396 ساعت: 6:36

صفحه بندی