java


Service crashes on app close


I am building an app to remind me stuff. I am using alarm manager for repeated alarms. But the problem when i set multiple alarms, if i close the app the first alarm service crashes, the second one works fine. I don't know why.
public void setReminder(){
Calendar dm = Calendar.getInstance();
AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(activity , broadcast.class);
intent.putExtra("com.todolist.dailylist.data" , listUID);
PendingIntent pendingintent = PendingIntent.getBroadcast(getApplicationContext(),alarmID, intent , PendingIntent.FLAG_ONE_SHOT);
int month = Integer.parseInt(splitDate(date)[1]);
dm.set(Calendar.MONTH, month -1 );
dm.set(Calendar.DAY_OF_MONTH, Integer.parseInt(splitDate(date)[0]));
dm.set(Calendar.YEAR, Integer.parseInt(splitDate(date)[2]));
dm.set(Calendar.HOUR, Integer.parseInt(formatTime(timeVar)[0]));
dm.set(Calendar.MINUTE, Integer.parseInt(formatTime(timeVar)[1]));
if(formatTime(timeVar)[2].equals("AM")) {
dm.set(Calendar.AM_PM, Calendar.AM);
}else if(formatTime(timeVar)[2].equals("PM")){
dm.set(Calendar.AM_PM, Calendar.PM);
}
long when = dm.getTimeInMillis();
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,when, AlarmManager.INTERVAL_DAY ,pendingintent);
}
Broadcast receiver code
public class broadcast extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String intentID = intent.getStringExtra("com.todolist.dailylist.data");
Intent service = new Intent(context, service.class);
service.putExtra("com.todolist.dailylist.listID", intentID);
context.startService(service);
}
}
Service shows a overlay window when the alarm triggers like the most to do apps.
Service class .
public class service extends Service {
private WindowManager windowManager;
private LayoutInflater inflater;
private View myview;
private diUtil databaseutil;
private String listID;
private String value = null;
private TextView listTitle;
private TextView listDescription;
private RelativeLayout rootLayout;
private RelativeLayout controllerIcons;
private ImageButton doneImage;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
databaseutil = new diUtil(this);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_TOAST,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.CENTER;
myview = inflater.inflate(R.layout.notificationwindow , null);
listTitle = (TextView)myview.findViewById(R.id.listTitleHeader);
listDescription = (TextView)myview.findViewById(R.id.listDescriptionHeader);
controllerIcons = (RelativeLayout)myview.findViewById(R.id.controller_icons);
rootLayout = (RelativeLayout)myview.findViewById(R.id.layout_controller);
doneImage = (ImageButton) myview.findViewById(R.id.doneIconController);
windowManager.addView(myview, params);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
listID = intent.getStringExtra("com.todolist.dailylist.listID");
ArrayList<listData> data = databaseutil.getListitem(listID);
for(int i=0; i < data.size();i++){
listTitle.setText(data.get(i).getListName());
if(data.get(i).getListText() != null){
Toast.makeText(this, "description present", Toast.LENGTH_SHORT).show();
}else if(data.get(i).getListText() == null){
Toast.makeText(this, "description not present", Toast.LENGTH_SHORT).show();
}
doneImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
windowManager.removeView(myview);
}
});
}
return Service.START_STICKY;
}
#Override
public void onDestroy() {
windowManager.removeView(myview);
super.onDestroy();
}
}
Here's the error i got from the logcat console
Process: com.todolist.dailylist, PID: 5601
java.lang.RuntimeException: Unable to start service com.todolist.dailylist.services.service#6a10f79 with null: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3314)
at android.app.ActivityThread.-wrap21(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at com.todolist.dailylist.services.service.onStartCommand(service.java:74)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3297)
at android.app.ActivityThread.-wrap21(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Anybody knows why this happens and how to solve this issue ?. Thank you so much for reading. Highly appreciated !.

Related Links

how to verify the sorting in collapse group selenium web driver-java
how to decide size of initial heap and maximum heap
JAVA: Virtual Mouse
Clean/Install Multiple maven projects in order without reactor
how to create a java jar file with source code (.java files)?
How to execute a script through sqlplus command using jsch?
SQL Syntax highligther in Java
Is this a proper use of recursion?
Modbus TCP in Java / Android
Web scraping through web pages using JSoup
my Search method is coming up with all nulls
Source Code Check Please - Macro Nutrient Calculator
FindItems (emails) restricted to date
Display images in a listview based on file type android
Android: HTML attachment not being sent with an email
why in java static final variable in interface can be overridden by a local var?

Categories

HOME
asp.net-core
drivers
tinymce
iot
jira
ravendb
serverspec
multiple-records
phaser
zebra-printers
percona
progressive-web-apps
reactive-cocoa
textfield
jtextfield
ml
riot.js
functional-testing
restful-authentication
one-to-many
traffic
rst2pdf
web-api-testing
neo4j-spatial
preg-match-all
http-get
hybridauth
android-ble
file-format
restlet
adobe-premiere
vapor
broadcastreceiver
hp-ux
quadratic-programming
checkboxlist
angular2-meteor
user-accounts
dropbox-php
git-diff
forever
lift-json
sonarlint-vs
grails-tomcat-plugin
azure-sdk
infix-notation
android-textview
elements
home-directory
phishing
sigabrt
dataview
nessus
rhino-servicebus
associative-array
operation
dundas
e10s
modalpopup
asp.net-web-api-odata
titanium-modules
elliptic-curve
html5-notifications
gnu-smalltalk
runtime.exec
ftps
flash-builder4.5
gwt-rpc
dataadapter
pushbackinputstream
wsdl-2.0
runas
venn-diagram
spyware
gcj
locationlistener
pyinotify
jquery-ui-droppable
bespin
compiler-specific
kdbg
ctp4
aquaticprime
mdac
wsdl.exe
windows-live-messenger

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App