Question

I am a newbie to android and I have asked this question earlier too. I have tried the solutions but it didn't help me. My service is giving me the following errors. I think the location in the onLocationChanged is set to null. I don't know how to solve this problem. Please help me. My codes and errors are as follows:

Error logs:

  03-22 07:22:33.019: E/AndroidRuntime(16440): FATAL EXCEPTION: main
  03-22 07:22:33.019: E/AndroidRuntime(16440): java.lang.NullPointerException
  03-22 07:22:33.019: E/AndroidRuntime(16440):  at com.example.broadcast.Ser.onLocationChanged(Ser.java:142)
  03-22 07:22:33.019: E/AndroidRuntime(16440):  at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:263)
  03-22 07:22:33.019: E/AndroidRuntime(16440):  at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:196)
  03-22 07:22:33.019: E/AndroidRuntime(16440):  at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:212)
  03-22 07:22:33.019: E/AndroidRuntime(16440):  at android.os.Handler.dispatchMessage(Handler.java:99)
  03-22 07:22:33.019: E/AndroidRuntime(16440):  at android.os.Looper.loop(Looper.java:137)
  03-22 07:22:33.019: E/AndroidRuntime(16440):  at android.app.ActivityThread.main(ActivityThread.java:4960)
  03-22 07:22:33.019: E/AndroidRuntime(16440):  at java.lang.reflect.Method.invokeNative(Native Method)
  03-22 07:22:33.019: E/AndroidRuntime(16440):  at java.lang.reflect.Method.invoke(Method.java:511)
 03-22 07:22:33.019: E/AndroidRuntime(16440):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
 03-22 07:22:33.019: E/AndroidRuntime(16440):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
  03-22 07:22:33.019: E/AndroidRuntime(16440):  at dalvik.system.NativeStart.main(Native Method)

And my service code is as follows:

    public class Ser extends Service implements LocationListener {
    public Location getLocation(String provider) {
    if (locationManager.isProviderEnabled(provider)) {
        locationManager.requestLocationUpdates(provider,
                MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
        if (locationManager != null) {
            location = locationManager.getLastKnownLocation(provider);

            if(location!=null){
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            String l1=String.valueOf(latitude);
            String l2=String.valueOf(longitude);
            }   
        }

        }


    return location;
}
@Override
public void onLocationChanged(Location location) {
    //this.location=location;
    //if(location!=null)
    //{
    double latitude =location.getLatitude();
    double longitude = location.getLongitude();
    DatabaseHandler db=new DatabaseHandler(getApplicationContext());
    String l1=String.valueOf(latitude);
    String l2=String.valueOf(longitude);
    val=db.taskid(l1, l2);
    String silent="Silent",vibrate="Vibrate";

    if(val.get("heading").compareTo(silent)==0)

{
        AudioManager audioManager= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
        audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); 
    }

    else if(val.get("heading").compareTo(vibrate)==0)
    {
        AudioManager audioManager= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
        audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); 
    }
    }

}

DatabaseHandler.class

  public class DatabaseHandler  extends SQLiteOpenHelper{
private static final String LOGCAT = null;
SQLiteDatabase db;
public DatabaseHandler(Context context) {
    super(context,"ishutup.db",null,4);

    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String query;
    query = "CREATE TABLE savedlocation (Id INTEGER PRIMARY KEY,locname TEXT,profile TEXT,cellid INT,lac INT,latitude DOUBLE,longitude DOUBLE)";
     db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    String query1;
    query1="DROP TABLE IF EXISTS savedlocation";
    db.execSQL(query1);
     onCreate(db);
}

 public HashMap<String, String> taskid(String lat,String lon) {

String selectQuery = "SELECT * FROM savedlocation WHERE latitude="+lat; 

HashMap<String, String> map = new HashMap<String, String>();
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
    do {


        map.put("heading", cursor.getString(1));


    } while (cursor.moveToNext());
}cursor.close();
database.close();

return map;
 }
Was it helpful?

Solution

val.get("heading") seems to be null . Try to debug and see if the database cursor not null and map should have a value added.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top