I'm trying to do my 1st notification and I'm getting an exception.
On onCreate()
method in main I call createNotification
of SchedulerActivity
class and in this procedure I call my BroadcastReceiver
class.
But I get an exception in SchedulerActivity
when I'm calling BroadcastReceiver
class on Intent myIntent = new Intent(SchedulerActivity.this, MyReceiver.class);
, and I can't understand why.
Exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.converter.android.daily/com.converter.android.daily.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:133) at android.content.ComponentName.(ComponentName.java:128)
at android.content.Intent.(Intent.java:4449)
at com.converter.android.daily.SchedulerActivity.createNotification(SchedulerActivity.java:272) at com.converter.android.daily.MainActivity.onCreate(MainActivity.java:39) at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Main.class
public class MainActivity extends BaseActivity {
private SchedulerActivity sch= new SchedulerActivity();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sch.createNotification(this.getApplicationContext());
}
SchedulerActivity.class
public class SchedulerActivity extends Activity implements PickerFragment.OnCompleteListener {
(...)
public void createNotification(Context context){
long when = System.currentTimeMillis();
String []parts;
String part1, part2;
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
status = pref.getString(botaoDaily, "");
time = pref.getString(dHora,"");
if (status.compareToIgnoreCase("On")==0) {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Daily")
.setContentText("My first Notification")
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent);
// .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notificationManager.notify(1, mNotifyBuilder.build());
if(time.compareToIgnoreCase("")!=0) {
parts = time.split(":");
part1 = parts[0];
part2 = parts[1];
/*hours = Integer.valueOf(part1);
minutes = Integer.valueOf(part2);
seconds = 0;*/
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 16);/*TODO: hours, minutes*/
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 15);
//Service -> MyAlarmService
Intent myIntent = new Intent(SchedulerActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(SchedulerActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 /* milliseconds */ * 20 /* seconds */, alarmIntent);
}
}
}
MyReceiver.class
public class MyReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
Can you please help me?