-1

刚开始在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();

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

4

3 回答 3

3

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

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

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

然后在MainActivity

new PostData(gps).execute();

AsyncTask只是一个与其他任何类一样的类,具有成员变量,如果您想使用它们,则需要设置它们。

于 2013-06-03T16:26:29.300 回答
0

您的问题是 GPS 只能在常规线程中收听,例如 UI 线程,而doInBackground()不是AsyncTask. 这与它需要 aLooper才能正常运行的事实有关。这是一篇与您的问题非常相似的好帖子。

首先获取您的 GPS 数据,onCreate然后您可以将该数据传输到您AsyncTask的可变参数并以doInBackground这种方式使用它。

于 2013-06-03T16:28:45.213 回答
0

gpsnull因为它没有被实例化。在doInBackground()你可以实例化它

gps = new GPSTracker();

但是由于您的GPSTracker构造函数需要 a Context,因此您必须向AsyncTas接受 a的构造函数添加一个构造函数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();
于 2013-06-03T16:29:23.000 回答