我是新來的android編程。我試圖提醒應用程序。但是當我將警報管理器添加到我的代碼中時。它給了我NullPointerException
。我檢查了之前提出的問題。但他們沒有幫助我。我還檢查了是否有我沒有初始化的東西。但我沒有找到。我也檢查了我的xml
。我認爲按鈕上可能存在問題()。但在那裏也沒有問題。我知道這個問題有很多問題。但你能幫我嗎?Android中的空指針異常
這是我ImageActivity類
public void addImage(View v)
{
int year = datepicker.getYear();
int month = datepicker.getMonth();
int day = datepicker.getDayOfMonth();
int hour = timepicker.getCurrentHour();
int minute = timepicker.getCurrentMinute();
Calendar c= Calendar.getInstance();
String uniqueid = System.currentTimeMillis()+"";
c.set(year,month,day,hour,minute);
long firstTime = c.getTimeInMillis();
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
mAlarmSender = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, firstTime, mAlarmSender);
db3.insertRecordimage(fileName2, String.valueOf(year), String.valueOf(month), String.valueOf(day), String.valueOf(hour), String.valueOf(minute));
}
這是我的XML
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:onClick="addImage"
android:id="@+id/button7"
android:layout_gravity="right" />
,這是我的logcat
06-05 22:29:49.916: E/AndroidRuntime(31354): at dalvik.system.NativeStart.main(Native Method)06-05 22:29:49.916: E/AndroidRuntime(31354): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 22:29:49.916: E/AndroidRuntime(31354): at java.lang.reflect.Method.invoke(Method.java:515)
06-05 22:29:49.916: E/AndroidRuntime(31354): at android.view.View$1.onClick(View.java:3964)
06-05 22:29:49.916: E/AndroidRuntime(31354): ... 11 more
06-05 22:29:49.916: E/AndroidRuntime(31354): Caused by: java.lang.NullPointerException
06-05 22:29:49.916: E/AndroidRuntime(31354): at com.example.pc.remindme.ImageActivity.addImage(ImageActivity.java:132)
06-05 22:29:49.916: E/AndroidRuntime(31354): ... 14 more
什麼是ImageActivity線132? – CommonsWare
它是「AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);」 – NewAndro
'context'爲空 – Blackbelt