0

我试图通过从 Telnet 传递值来获得经纬度位置。完成之后,现在我尝试实施反向地理编码以获取模拟器中的 apporx 地址。 关注了这个链接

但它显示应用程序流意外停止。

日志猫错误..

05-19 16:51:14.292: E/AndroidRuntime(460): java.lang.NullPointerException
05-19 16:51:14.292: E/AndroidRuntime(460):  at com.gps.HelloAndroidGpsActivity$ReverseGeocodeLookupTask.onPostExecute(HelloAndroidGpsActivity.java:137)
05-19 16:51:14.292: E/AndroidRuntime(460):  at com.gps.HelloAndroidGpsActivity$ReverseGeocodeLookupTask.onPostExecute(HelloAndroidGpsActivity.java:1)
05-19 16:51:14.292: E/AndroidRuntime(460):  at android.os.AsyncTask.finish(AsyncTask.java:417)
05-19 16:51:14.292: E/AndroidRuntime(460):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
05-19 16:51:14.292: E/AndroidRuntime(460):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
05-19 16:51:14.292: E/AndroidRuntime(460):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 16:51:14.292: E/AndroidRuntime(460):  at android.os.Looper.loop(Looper.java:130)
05-19 16:51:14.292: E/AndroidRuntime(460):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-19 16:51:14.292: E/AndroidRuntime(460):  at java.lang.reflect.Method.invokeNative(Native Method)
05-19 16:51:14.292: E/AndroidRuntime(460):  at java.lang.reflect.Method.invoke(Method.java:507)
05-19 16:51:14.292: E/AndroidRuntime(460):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-19 16:51:14.292: E/AndroidRuntime(460):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-19 16:51:14.292: E/AndroidRuntime(460):  at dalvik.system.NativeStart.main(Native Method)

...

HelloAndroidGpsActivity.java

package com.gps;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.gps.GeoCodeResult;
import com.gps.GeoCoder;



public class HelloAndroidGpsActivity extends Activity {

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

    private GeoCoder geoCoder = new GeoCoder();

    protected LocationManager locationManager;
    protected Location currentLocation;

    protected Button retrieveLocationButton;
    protected Button reverseGeocodingButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
        reverseGeocodingButton = (Button) findViewById(R.id.reverse_geocoding_button);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 
                MINIMUM_TIME_BETWEEN_UPDATES, 
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                new MyLocationListener()
        );

        retrieveLocationButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showCurrentLocation();
            }
        });

        reverseGeocodingButton.setOnClickListener(new OnClickListener() {            
            public void onClick(View v) {                
                performReverseGeocodingInBackground();
            }
        });

    }    

    protected void performReverseGeocodingInBackground() {
        showCurrentLocation();
        new ReverseGeocodeLookupTask().execute((Void[])null);
    }

    protected void showCurrentLocation() {

        currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (currentLocation != null) {
            String message = String.format(
                    "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                    currentLocation.getLongitude(), currentLocation.getLatitude()
            );
            Toast.makeText(HelloAndroidGpsActivity.this, message,
                    Toast.LENGTH_LONG).show();
        }

    }   

    private class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {
            String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
            Toast.makeText(HelloAndroidGpsActivity.this, message, Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String s, int i, Bundle b) {
            Toast.makeText(HelloAndroidGpsActivity.this, "Provider status changed",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) {
            Toast.makeText(HelloAndroidGpsActivity.this,
                    "Provider disabled by the user. GPS turned off",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) {
            Toast.makeText(HelloAndroidGpsActivity.this,
                    "Provider enabled by the user. GPS turned on",
                    Toast.LENGTH_LONG).show();
        }

    }

    public class ReverseGeocodeLookupTask extends AsyncTask <Void, Void, GeoCodeResult> {

        private ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            this.progressDialog = ProgressDialog.show(
                    HelloAndroidGpsActivity.this,
                    "Please wait...contacting Yahoo!", // title
                    "Requesting reverse geocode lookup", // message
                    true // indeterminate
            );
        }

        @Override
        protected GeoCodeResult doInBackground(Void... params) {
            return geoCoder.reverseGeoCode(currentLocation.getLatitude(), currentLocation.getLongitude());
        }

        @Override
        protected void onPostExecute(GeoCodeResult result) {

            Toast.makeText(HelloAndroidGpsActivity.this, result.toString(), Toast.LENGTH_LONG).show();            
        }

    }

}

地理编码器.java

*package com.gps;
import com.gps.GeoCodeResult;
import com.gps.HttpRetriever;
import com.gps.XmlParser;
public class GeoCoder {

    private static final String YAHOO_API_BASE_URL = "http://where.yahooapis.com/geocode?q=%1$s,+%2$s&gflags=R&appid=[yourappidhere]"; **//This is correct API**
       private HttpRetriever httpRetriever = new HttpRetriever();
        private XmlParser xmlParser = new XmlParser();

        public GeoCodeResult reverseGeoCode(double latitude, double longitude) {

            String url = String.format(YAHOO_API_BASE_URL, String.valueOf(latitude), String.valueOf(longitude));        
            String response = httpRetriever.retrieve(url);
            return xmlParser.parseXmlResponse(response);

        }
    }*

地理编码结果

package com.gps;
public class GeoCodeResult {

    public String line1;
    public String line2;
    public String line3;
    public String line4;

    @Override
    public String toString() {

        StringBuilder builder = new StringBuilder();
        builder.append("Location:");

        if (line1!=null)
            builder.append("-"+line1);
        if (line2!=null)
            builder.append("-"+line2);
        if (line3!=null)
            builder.append("-"+line3);
        if (line4!=null)
            builder.append("-"+line4);

        return builder.toString();

    }

}

问题是地理编码结果为空

我错过了什么吗?点我是我做错了。谢谢

4

2 回答 2

0

检查你的android表现文件(manifest.xml),看看你是否调用了互联网使用权限,它看起来像这样:

<uses-permission android:name="android.permission.INTERNET" />

并尝试<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />使用 GPS 外壳。

于 2012-05-19T12:46:43.727 回答
0

有时它会发生。它给我们null。同样的问题是在我的情况下。在其他设备上或一段时间后尝试。尝试每分钟只访问一次服务器地址。一分钟内请求地址超过一个,可能导致'null'

于 2012-05-31T10:52:27.870 回答