Android: Unable to start service with null: java.lang.NullPointerException -


before begin, want make clear have read previous answers similar topics (mainly this one) , answer given in comments didn't work me (adding intent != null before intent.getaction() didn't work).

i'm trying make foreground service mayan horoscope. purpose of service read horoscope symbols , prediction sqlite database , whenever recieves text message following: "maya dd/mm/yyyy" (the dob), service obtain info , send text message automatically. have gotten work background service without error, want give notification user can see if service running or not.

i have followed other tutorials (tutorial 1 , tutorial 2) make foreground service , works fine without error.

my problem whenever start service, stop service , clear ram (killing app) nullpointer exception time on service's line 57 (no matter code have it's line 57). on phone gives me message "unfortunately horoscopomaya has stopped working." twice!

manifest.xml

<?xml version="1.0" encoding="utf-8"?> 

<application     android:allowbackup="true"     android:icon="@mipmap/ic_launcher"     android:label="@string/app_name"     android:supportsrtl="true"     android:theme="@style/apptheme">     <activity android:name=".mainactivity"         android:launchmode="singletask">         <intent-filter>             <action android:name="android.intent.action.main" />              <category android:name="android.intent.category.launcher" />         </intent-filter>     </activity>     <service android:name=".servicioastralmaya"              android:exported="false" /> </application> <uses-permission android:name="android.permission.send_sms" /> <uses-permission android:name="android.permission.read_sms" /> <uses-permission android:name="android.permission.receive_sms" /> 

mainactivity.java:

public void startservice( view view ) {     if( brunning == false )     {         intent intent = new intent( mainactivity.this, servicioastralmaya.class );         intent.setaction( constants.action.start_foreground_action );         startservice( intent );         txtvstatserv.settext( "invocados " );         brunning = true;     } }//end startservice( view )  public void stopservice( view view ) {     if( brunning == true )     {         intent intent = new intent( mainactivity.this, servicioastralmaya.class );         intent.setaction( constants.action.stop_foreground_action );         startservice( intent );         txtvstatserv.settext( "descansando " );         brunning = false;     } }//end stopservice( view ) 

servicioastralmaya.java (service):

@override public int onstartcommand( intent intent, int flags, int startid ) {     if( intent != null && intent.getaction().equals( constants.action.start_foreground_action     ) )     {         log.i( tag, "servicio iniciado!" );         isrunning = true;         startforeground( constants.notification_id.service_id, getcompatnotification() );     }else if( intent != null && intent.getaction().equals( constants.action.stop_foreground_action ) )     {          log.i( tag, "servicio detenido!" );         isrunning = false;         stopforeground( true );         stopself();     }     return start_sticky; }  private notification getcompatnotification() {     notificationcompat.builder builder = new notificationcompat.builder( );     builder.setsmallicon( r.drawable.homonculus_32 )         .setcontenttitle( "horoscopos maya" )         .setticker( "astros conectados" )         .setwhen( system.currenttimemillis() );     intent startintent = new intent( this, mainactivity.class );     pendingintent contentintent = pendingintent.getactivity( this, 0, startintent,         0 );     builder.setcontentintent( contentintent );     notification notification = builder.build();      return notification; }//end notification getcompatnotification()  @override public void ondestroy() {     super.ondestroy();     isrunning = false; } 

constants.java

public interface action {     public static final string start_foreground_action = "edu.ito.jmhg" +         ".horoscopomaya.action.start_foreground";     public static final string stop_foreground_action = "edu.ito.jmhg" +         ".horoscopomaya.action.stop_foreground"; }  public interface notification_id {     public static final int service_id = 1; } 

line 57 this: log.i( tag, "servicio iniciado!" );

the isrunning , brunning check if service running show status in mainactivity:

brunning = isservicerunning( servicioastralmaya.class );  private boolean isservicerunning( class<?> serviceclass ) {     activitymanager manager = (activitymanager)getsystemservice( context.activity_service );     for( activitymanager.runningserviceinfo service : manager.getrunningservices( integer         .max_value ) )     {         if( serviceclass.getname().equals( service.service.getclassname() ) ) {             return true;         }     }     return false; }//end boolean isservicerunning( class<?> ) 

and in service: public static boolean isrunning;

i have tried calling stopservice , doing stopforeground(true); , stopself(); in ondestroy() method still no avail. , error still line 57 of service class.

logcat:

fatal exception: main process: edu.ito.jmhg.horoscopomaya, pid: 21642 java.lang.runtimeexception: unable start service  edu.ito.jmhg.horoscopomaya.servicioastralmaya@426d11c8 null: java.lang.nullpointerexception ... caused by: java.lang.nullpointerexception @ edu.ito.jmhg.horoscopomaya.servicioastralmaya.onstartcommand(servicioastralmaya.java:57) 

i running android kitkat 4.4.4 (api 19) android studio 2 using rooted samsung grand prime.

edit

i don't know if relevant, did notice android studio. no matter changes make, when run app notified "no changes deploy"

event log:

19:32:47 executing tasks: [:app:assembledebug] 19:33:50 gradle build finished in 1m 3s 59ms 19:33:51 no changes deploy 

i've changed code of service , line error still 57, ) ) right after if( intent.getaction().equals( constants.action.start_foreground_action

servicioastralmaya.java (service):

@override public int onstartcommand( intent intent, int flags, int startid ) {     if( intent != null && intent.getaction() != null )     {         if( intent.getaction().equals( constants.action.start_foreground_action         ) )         {             log.i( tag, "servicio iniciado!" );             isrunning = true;             startforeground( constants.notification_id.service_id, getcompatnotification() );         }else if( intent.getaction().equals( constants.action.stop_foreground_action ) )         {             log.i( tag, "servicio detenido!" );             isrunning = false;             stopforeground( true );             stopself();         }     }     return start_sticky; } 

also, since i've changed android 2 gradle updated , had weird error had change buildtoolsversion in build.gradle buildtoolsversion "24.0 rc2" buildtoolsversion "23.0.2"

build.gradle:

apply plugin: 'com.android.application' android { compilesdkversion 23 buildtoolsversion "23.0.2"  defaultconfig {     applicationid "edu.ito.jmhg.horoscopomaya"     minsdkversion 19     targetsdkversion 23     versioncode 1     versionname "1.0" } buildtypes {     release {         minifyenabled false         proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'     } } } dependencies { compile filetree(dir: 'libs', include: ['*.jar']) testcompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' } 

the problem android studio itself!!

like said in edit, android studio gave me "no changes deploy", strange, searched , found this solution

just go "file -> settings -> build, execution, deployement -> instant run" , disable it. android studio builds scratch each time it's better not building right.

i kept if condition mentioned @francesc on safe side , app doesn't give me nullpointerexception anymore.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -