我正在使用Android服务开发Android push notification
。
当我从datepicker
中选择日期/月/年时,它就像魅力一样(请参阅下面的代码)。但是当我尝试将calender instance
设置为hard code
时,它会给我一个 NULL指针异常。
我是怎么做的:
我为此尝试了两种方法,但是我得到了例外。
1。方法
Calendar c = Calendar.getInstance();
c.clear();
int year = 2014;
int month = 3;
int day = 21;
c.set(year, month, day);
2。方法
Calendar c = Calendar.getInstance();
Calendar dt = Calendar.getInstance();
dt.clear();
dt.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE));
MainActivity:
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
答案 0 :(得分:1)
当服务尚未绑定时,您无法设置日历。 你应该倾听onServiceConnected。
在具体示例中,您应该在ScheduleClient.java:47中设置日历 这就是服务连接的时刻,可以设置日历。
(ScheduleClient.java)的某些内容:
public class ScheduleClient {
// The hook into our service
private ScheduleService mBoundService;
// The context to start the service in
private Context mContext;
// A flag if we are connected to the service or not
private boolean mIsBound;
public ScheduleClient(Context context) {
mContext = context;
}
/**
* Call this to connect your activity to your service
*/
public void doBindService() {
// Establish a connection with our service
mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
/**
* When you attempt to connect to the service, this connection will be called with the result.
* If we have successfully connected we instantiate our service object so that we can call methods on it.
*/
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with our service has been established,
// giving us the service object we can use to interact with our service.
mBoundService = ((ScheduleService.ServiceBinder) service).getService();
Calendar c = Calendar.getInstance();
c.clear();
int year = 2014;
int month = 3;
int day = 21;
c.set(year, month, day);
ScheduleClient.this.setAlarmForNotification(c);
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
/**
* Tell our service to set an alarm for the given date
* @param c a date to set the notification for
*/
public void setAlarmForNotification(Calendar c){
mBoundService.setAlarm(c);
}
/**
* When you have finished with the service call this method to stop it
* releasing your connection and resources
*/
public void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
mContext.unbindService(mConnection);
mIsBound = false;
}
}
}