为什么我在OnLocationchanged()上得到一个nullpointer异常?

时间:2014-03-22 01:56:08

标签: android service forceclose locationlistener

我是android的新手,我之前也提过过这个问题。我尝试了解决方案,但它没有帮助我。我的服务给了我以下错误。我认为onLocationChanged中的位置设置为null。我不知道如何解决这个问题。请帮我。我的代码和错误如下:

错误日志:

  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)

我的服务代码如下:

    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;
 }

1 个答案:

答案 0 :(得分:1)

val.get(“heading”)似乎为null。尝试调试并查看数据库游标是否为null,并且map应该添加一个值。