AsyncTask中的android空指针异常

时间:2013-06-03 16:24:11

标签: android gps nullpointerexception postdata

我刚刚开始在android下编写并遇到了我不知道如何解决的问题

PostData.java

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;

import android.os.AsyncTask;
import android.text.format.Time;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

class PostData extends AsyncTask<Void, Void, Void> {
    // GPSTracker class
        GPSTracker gps;
    //определяем переменную главного активити
        MainActivity ma;
        Teleport_user_profile_activity UP;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            // do stuff before posting data
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            postData();
            return null;
        }


        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            // do stuff after posting data
            super.onPostExecute(result);
        }



        public void postData() {
            // TODO Auto-generated method stub
            // Create a new HttpClient and Post Header
            //переводим значение double в стринг
            double latitudep = gps.getLatitude();
            double longitudep = gps.getLongitude();
        double totalLatitude = latitudep;
            String stotalLatitude = String.valueOf(totalLatitude);
            double totalLongitude = longitudep;
            String stotalLongitude = String.valueOf(totalLongitude);
            // временная переменная для определения времени устройства
            Time nowTime = new Time();
            nowTime.setToNow();
            String snowTime = String.valueOf(nowTime);
            //берем информацию о юзере
                UP.LoadUserInfoFromFile();
                UP.LoadUserInfopassFromFile();
                String UserInfo = Teleport_user_profile_activity.UserLoginFile;
                String UserPass = Teleport_user_profile_activity.UserPassFile;
            //посылка данных на сервер
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://myheart.pp.ua/Android_in.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);

//          nameValuePairs.add(new BasicNameValuePair("latitude", stotalLatitude));
//          nameValuePairs.add(new BasicNameValuePair("longitude", stotalLongitude));
            nameValuePairs.add(new BasicNameValuePair("Android_device_time", snowTime));
//          nameValuePairs.add(new BasicNameValuePair("user_info", UserInfo));
//          nameValuePairs.add(new BasicNameValuePair("user_pass", UserPass));
            nameValuePairs.add(new BasicNameValuePair("separator", "______________________________________"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpclient.execute(httppost);

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

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

}
}

MainActivity.java

 R.id.menu_send_location_manual: {

             GPSdetermination();
             new PostData().execute();


            // postData(); - вызывает ошибку на реальном устройстве
             Toast.makeText(getApplicationContext(), R.string.location_send_manuall_toast, Toast.LENGTH_LONG).show();
        break;
    }

 public void GPSdetermination() {       
     // create class object
     gps = new GPSTracker(MainActivity.this);

     // check if GPS enabled        
     if(gps.canGetLocation()){

         double latitude = gps.getLatitude();
         double longitude = gps.getLongitude();

         // \n is for new line
         Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();   
     }else{
         // can't get location
         // GPS or Network is not enabled
         // Ask user to enable GPS/network in settings
         gps.showSettingsAlert();
     }

 } 

GPSTracker.java

    public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

//определяем переменную главного активити
    MainActivity ma;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

// Функция для определения местоположения
public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}
/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 * */
public void stopUsingGPS(){
    if(locationManager != null){
        locationManager.removeUpdates(GPSTracker.this);
    }       
}

/**
 * Function to get latitude
 * */
public double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

/**
 * Function to check GPS/wifi enabled
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog
 * On pressing Settings button will lauch Settings Options
 * */
public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS is settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}


//события которые происходят если позиция поменялась
@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

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

当我尝试调用PostData时,我在此代码中收到错误java.lang.NullPointerException

double latitudep = gps.getLatitude();
double longitudep = gps.getLongitude();

似乎无法弄清楚如何解决它。

3 个答案:

答案 0 :(得分:3)

您已在AsyncTask中声明GPSTracker gps;,它似乎永远不会被初始化。换句话说,它是空的。因此,NullPointerException

我猜你需要一个AsyncTask的构造函数来设置它:

public PostData (GPSTracker gps) {
    this.gps = gps;
}

然后在MainActivity

new PostData(gps).execute();

AsyncTask只是一个类,与成员变量一样,需要设置,如果你想使用它们。

答案 1 :(得分:0)

您的问题是GPS只能在常规线程中收听,例如UI线程,而不是doInBackground()的{​​{1}}方法。这与需要AsyncTask正常运行的事实有关。这是good post,与您的问题非常相似。

首先在Looper获取您的GPS数据,然后您可以将这些数据作为varargs传输到onCreate并以AsyncTask方式使用它。

答案 2 :(得分:0)

gpsnull,因为它未实例化。在doInBackground()中,您可以实例化它

gps = new GPSTracker();

但由于您的GPSTracker构造函数需要Context,因此您必须向AsyncTas添加一个接受Context

的构造函数

异步

pubic class PostData extends AsyncTask<Void, Void, Void> {
   Context mContext;

    MainActivity ma;
    Teleport_user_profile_activity UP;
    public PostData(Context c)
   {
         mContext = c;
   }

然后在doInBackground()

@Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        gps = new GPSTracker(mContext);
        postData();
        return null;
    }

在MainActivity中调用你的AsyncTask

 R.id.menu_send_location_manual: {

         GPSdetermination();
         PostData task = new PostData(MainActivity.this);
         task.execute();
         new PostData().execute();