2

这是被调用的代码:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);


    context=this;
    Timer timer=new Timer();
    TimerTask task=new TimerTask() {

        @Override
        public void run() {
            MyLocation myLocation=new MyLocation();
            try {
                MyLocation.getLocation(context, new MyLocationListener() {

                    @Override
                    public void gotLocation(Location location) {
                        SharedPreferences pref=getSharedPreferences(Utilities.USER_PREF, MODE_PRIVATE);
                        SharedPreferences.Editor editor=pref.edit();
                        double lat=location.getLatitude();
                        double lon=location.getLongitude();
                        editor.putString("lat", lat+"");
                        editor.putString("lon", lon+"");
                        Geocoder coder=new Geocoder(MyLocationService.this);
                        try {
                            List<Address> addresses=coder.getFromLocation(lat, lon, 1);
                            String address=addresses.get(0).getAddressLine(0)+","+addresses.get(0).getAdminArea();
                            editor.putString("address", address+"");
                            editor.commit();

                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            editor.commit();
                        }

                    }
                });
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

        }
    };
    timer.schedule(task, 1000);

    return Service.START_STICKY;
}

这是抛出的异常:

03-10 03:01:25.721: E/AndroidRuntime(912): FATAL EXCEPTION: Timer-0
03-10 03:01:25.721: E/AndroidRuntime(912):  at com.barkleyandpaws.services.MyLocationService$1.run(MyLocationService.java:78)
03-10 03:01:25.721: E/AndroidRuntime(912): java.lang.NullPointerException

我只是不确定发生了什么......我非常感谢您可能拥有的任何见解。代码的目的是启动一个服务和计时器,该服务和计时器获取周期性位置信息并将其存储在变量中,其他方法使用这些变量在地图上显示或按距离对信息进行排序。

这是 MyLocationService.java 的完整代码

package com.barkleyandpaws.services;

import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.IBinder;
import android.util.Log;

import com.barkleyandpaws.MyLocation;
import com.barkleyandpaws.MyLocation.MyLocationListener;
import com.barkleyandpaws.utils.Utilities;

public class MyLocationService extends Service {

    Context context;

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

//  @Override
//  public void onCreate() {
//      //Log.i(XXX.LOG, "onCreate");
//      super.onCreate();
//  }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);


        context=this;
        Timer timer=new Timer();
        TimerTask task=new TimerTask() {

            @Override
            public void run() {
                MyLocation myLocation=new MyLocation();         
                try {
                    MyLocation.getLocation(context, new MyLocationListener() {

                        @Override
                        public void gotLocation(Location location) {
                            SharedPreferences pref=getSharedPreferences(Utilities.USER_PREF, MODE_PRIVATE);
                            SharedPreferences.Editor editor=pref.edit();
                            double lat=location.getLatitude();
                            double lon=location.getLongitude();
                            editor.putString("lat", lat+"");
                            editor.putString("lon", lon+"");
                            Geocoder coder=new Geocoder(MyLocationService.this);
                            try {
                                List<Address> addresses=coder.getFromLocation(lat, lon, 1);
                                String address=addresses.get(0).getAddressLine(0)+","+addresses.get(0).getAdminArea();
                                editor.putString("address", address+"");
                                editor.commit();

                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                editor.commit();
                            }

                        }
                    });
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }

            }
        };
        timer.schedule(task, 1000);

        return Service.START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


}
4

0 回答 0