Wednesday, 7 August 2013

Nullpointer at creating Intent

Nullpointer at creating Intent

I get a nullpointer at PendingIntent pi =
PendingIntent.getService(context, 0, new Intent( context,
MyNotification.class), PendingIntent.FLAG_UPDATE_CURRENT); But I have no
idea how to fix this.. I hope someone might be able to help me..
What it should do is set the alarm everyday at 9 so I can set a
notification in the MyNotification class which contains a
BroadcastReceiver.
Below is all my code:
Calling the AlarmManager class (From MainActivity):
Alarm setAlarm = new Alarm();
setAlarm.setRecurringAlarm();
The class where I want to set the AlarmManager (Alarm.class):
public class Alarm extends Activity {
public void setRecurringAlarm() {
Context context = getBaseContext();
Log.i("Alarm", "Setting Recurring Alarm");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(
context, MyNotification.class),
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
}}
My Log:
08-07 22:43:38.079: E/AndroidRuntime(21476): FATAL EXCEPTION: main 08-07
22:43:38.079: E/AndroidRuntime(21476): java.lang.RuntimeException: Unable
to start activity
ComponentInfo{com.weatherclothes/com.weatherclothes.MainActivity}:
java.lang.NullPointerException 08-07 22:43:38.079:
E/AndroidRuntime(21476): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
08-07 22:43:38.079: E/AndroidRuntime(21476): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
08-07 22:43:38.079: E/AndroidRuntime(21476): at
android.app.ActivityThread.access$600(ActivityThread.java:151) 08-07
22:43:38.079: E/AndroidRuntime(21476): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335) 08-07
22:43:38.079: E/AndroidRuntime(21476): at
android.os.Handler.dispatchMessage(Handler.java:99) 08-07 22:43:38.079:
E/AndroidRuntime(21476): at android.os.Looper.loop(Looper.java:155) 08-07
22:43:38.079: E/AndroidRuntime(21476): at
android.app.ActivityThread.main(ActivityThread.java:5493) 08-07
22:43:38.079: E/AndroidRuntime(21476): at
java.lang.reflect.Method.invokeNative(Native Method) 08-07 22:43:38.079:
E/AndroidRuntime(21476): at
java.lang.reflect.Method.invoke(Method.java:511) 08-07 22:43:38.079:
E/AndroidRuntime(21476): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
08-07 22:43:38.079: E/AndroidRuntime(21476): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795) 08-07
22:43:38.079: E/AndroidRuntime(21476): at
dalvik.system.NativeStart.main(Native Method) 08-07 22:43:38.079:
E/AndroidRuntime(21476): Caused by: java.lang.NullPointerException 08-07
22:43:38.079: E/AndroidRuntime(21476): at
android.content.ComponentName.(ComponentName.java:75) 08-07 22:43:38.079:
E/AndroidRuntime(21476): at android.content.Intent.(Intent.java:3655)
08-07 22:43:38.079: E/AndroidRuntime(21476): at
com.weatherclothes.Alarm.setRecurringAlarm(Alarm.java:23) 08-07
22:43:38.079: E/AndroidRuntime(21476): at
com.weatherclothes.MainActivity.onCreate(MainActivity.java:58) 08-07
22:43:38.079: E/AndroidRuntime(21476): at
android.app.Activity.performCreate(Activity.java:5066) 08-07 22:43:38.079:
E/AndroidRuntime(21476): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
08-07 22:43:38.079: E/AndroidRuntime(21476): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
08-07 22:43:38.079: E/AndroidRuntime(21476): ... 11 more

No comments:

Post a Comment