Android 为日历实例设置硬代码值后出现空指针异常
我正在使用android服务开发androidAndroid 为日历实例设置硬代码值后出现空指针异常,android,push-notification,android-notifications,android-calendar,Android,Push Notification,Android Notifications,Android Calendar,我正在使用android服务开发android推送通知 当我从日期选择器中选择日期/月/年时,它就像一个符咒(请参见下面的代码)。但是,当我试图用硬代码值设置日历实例时,它会给我一个空指针异常 我是如何尝试的: private ScheduleClient scheduleClient; private SharedPreferences prefs; private String prefName = "userPrefs"; private static final String TIT
推送通知
当我从日期选择器
中选择日期/月/年时,它就像一个符咒(请参见下面的代码)。但是,当我试图用硬代码
值设置日历实例
时,它会给我一个空指针异常
我是如何尝试的:
private ScheduleClient scheduleClient;
private SharedPreferences prefs;
private String prefName = "userPrefs";
private static final String TITLE_KEY = "title";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a new service client and bind our activity to this service
scheduleClient = new ScheduleClient(this);
scheduleClient.doBindService();
t1 = (TextView) findViewById(R.id.txt);
nofitication();
}
private void nofitication(){
Calendar c = Calendar.getInstance();
c.clear();
int year = 2014;
int month = 3;
int day = 21;
c.set(year, month, day);
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//---save the values in the EditText view to preferences---
editor.putString(TITLE_KEY,"Muneeb's Notification");
//---saves the values---
editor.commit();
scheduleClient.setAlarmForNotification(c);
// Toast.makeText(this, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();
}
scheduleClient.setAlarmForNotification(c);
我为此尝试了两种方法,但从这两种方法中我都得到了一个例外
1。方法
Calendar c = Calendar.getInstance();
c.clear();
int year = 2014;
int month = 3;
int day = 21;
c.set(year, month, day);
Calendar c = Calendar.getInstance();
Calendar dt = Calendar.getInstance();
dt.clear();
dt.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE));
2。方法
Calendar c = Calendar.getInstance();
c.clear();
int year = 2014;
int month = 3;
int day = 21;
c.set(year, month, day);
Calendar c = Calendar.getInstance();
Calendar dt = Calendar.getInstance();
dt.clear();
dt.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE));
main活动:
private ScheduleClient scheduleClient;
private SharedPreferences prefs;
private String prefName = "userPrefs";
private static final String TITLE_KEY = "title";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a new service client and bind our activity to this service
scheduleClient = new ScheduleClient(this);
scheduleClient.doBindService();
t1 = (TextView) findViewById(R.id.txt);
nofitication();
}
private void nofitication(){
Calendar c = Calendar.getInstance();
c.clear();
int year = 2014;
int month = 3;
int day = 21;
c.set(year, month, day);
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//---save the values in the EditText view to preferences---
editor.putString(TITLE_KEY,"Muneeb's Notification");
//---saves the values---
editor.commit();
scheduleClient.setAlarmForNotification(c);
// Toast.makeText(this, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();
}
scheduleClient.setAlarmForNotification(c);
此行出现错误:
private ScheduleClient scheduleClient;
private SharedPreferences prefs;
private String prefName = "userPrefs";
private static final String TITLE_KEY = "title";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a new service client and bind our activity to this service
scheduleClient = new ScheduleClient(this);
scheduleClient.doBindService();
t1 = (TextView) findViewById(R.id.txt);
nofitication();
}
private void nofitication(){
Calendar c = Calendar.getInstance();
c.clear();
int year = 2014;
int month = 3;
int day = 21;
c.set(year, month, day);
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//---save the values in the EditText view to preferences---
editor.putString(TITLE_KEY,"Muneeb's Notification");
//---saves the values---
editor.commit();
scheduleClient.setAlarmForNotification(c);
// Toast.makeText(this, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();
}
scheduleClient.setAlarmForNotification(c);
当我运行带有硬代码值的代码时,带有的c
将始终变为null.(参见下图)
logcat
04-21 10:07:39.430: E/AndroidRuntime(4721): FATAL EXCEPTION: main
04-21 10:07:39.430: E/AndroidRuntime(4721): Process: com.example.customnotification, PID: 4721
04-21 10:07:39.430: E/AndroidRuntime(4721): java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.customnotification/com.example.customnotification.TestingActivity}: java.lang.NullPointerException
04-21 10:07:39.430: E/AndroidRuntime(4721): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
04-21 10:07:39.430: E/AndroidRuntime(4721): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
04-21 10:07:39.430: E/AndroidRuntime(4721): at android.app.ActivityThread.access$800(ActivityThread.java:156)
04-21 10:07:39.430: E/AndroidRuntime(4721): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
04-21 10:07:39.430: E/AndroidRuntime(4721): at android.os.Handler.dispatchMessage(Handler.java:102)
04-21 10:07:39.430: E/AndroidRuntime(4721): at android.os.Looper.loop(Looper.java:157)
04-21 10:07:39.430: E/AndroidRuntime(4721): at android.app.ActivityThread.main(ActivityThread.java:5872)
04-21 10:07:39.430: E/AndroidRuntime(4721): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 10:07:39.430: E/AndroidRuntime(4721): at java.lang.reflect.Method.invoke(Method.java:515)
04-21 10:07:39.430: E/AndroidRuntime(4721): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
04-21 10:07:39.430: E/AndroidRuntime(4721): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
04-21 10:07:39.430: E/AndroidRuntime(4721): at dalvik.system.NativeStart.main(Native Method)
04-21 10:07:39.430: E/AndroidRuntime(4721): Caused by: java.lang.NullPointerException
04-21 10:07:39.430: E/AndroidRuntime(4721): at services.ScheduleClient.setAlarmForNotification(ScheduleClient.java:62)
04-21 10:07:39.430: E/AndroidRuntime(4721): at com.example.customnotification.TestingActivity.nofitication(TestingActivity.java:56)
04-21 10:07:39.430: E/AndroidRuntime(4721): at com.example.customnotification.TestingActivity.onCreate(TestingActivity.java:31)
04-21 10:07:39.430: E/AndroidRuntime(4721): at android.app.Activity.performCreate(Activity.java:5312)
04-21 10:07:39.430: E/AndroidRuntime(4721): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
04-21 10:07:39.430: E/AndroidRuntime(4721): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
04-21 10:07:39.430: E/AndroidRuntime(4721): ... 11 more
尚未绑定服务时,无法设置日历。
你应该听我说
在您的具体示例中,您应该在ScheduleClient.java:47中设置日历
这是连接服务和设置日历的时刻
类似于(ScheduleClient.java)的内容:
}您的屏幕截图没有真正显示c
为null
。相反,Exception e
为null
。您可以发布setAlarmForNotification()
的代码吗?另外,为NullPointerException
发布日志猫。现在我已经上传了日志猫,请检查日志猫。@AndrewT。谢谢你的回复setAlarmForNotification
是服务类中的函数。由以下原因引起:java.lang.NullPointerException at services.ScheduleClient.setAlarmForNotification(ScheduleClient.java:62)
。如果可能,检查并将代码张贴在ScheduleClient
mBoundService.setAlarm(c)内的第62行代码>这是ScheduleClient.class中的行号62