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

I have a video gallery try to get videos files from a specific directory i am getting an error parameters cannot be resolved to a variable
rx java observable chaining on only matched observables
More concise solution using regex in java
Gregorian Calendar month of year [duplicate]
Trying to bring the properties/values such as the names or dates of an XML file into java from a directory
Java JEditorPane: detecting failure to connect to site
Comparing two string in file java
Java swing method setLayout null doesn't work
Eclipse is not creating default project and main layout in ADT
Hibernate: MySQL
Get actual time (java, c) synchronized
Load database values to Vector
Eclipse / Android Development Studio Guidelines for both Android and non-Android development
Moving recursively in arrays [closed]
use private to specify access control of a class
Setting up hadoop on azure

Categories

HOME
testing
google-chrome-extension
gremlin
mediawiki
google-api-php-client
grep
elm
spring-jdbc
sharepoint-designer
baqend
networkx
here-api
php-7.1
gz
task
circuit
django-simple-history
aurigma
evopdf
physics-engine
extjs5
sqlcipher
su
facebook-access-token
chromebook
ping
dbclient
calibre
accelerate-framework
geopositioning
mmenu
sequential
bytecode-manipulation
host
devextreme
dartium
nxlog
vao
flashair
businessworks
theming
python-c-api
boost-multi-index
gabor-filter
git-diff
lowpass-filter
nativeapplication
nodebb
typed-lambda-calculus
pintos
word-vba-mac
py2app
impresspages
simplewebrtc
historian
maven-tomcat-plugin
ruby-2.2
ionic
responsive-images
ideamart
streambase
kineticjs
formatjs
file-copying
heisenbug
starcluster
p4java
jubula
rebol3
valueconverter
cloud-connect
srs
fmod
access-rights
pvrtc
jquery-knob
newtonscript
removeclass
boost-filesystem
fireworks
external-accessory
dmx512
bespin
ctp4
avatar
zend-decorators
uiq3

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