当我尝试在android 2.3中检索de Reverse Geocoding时退出

时间:2012-05-19 11:24:22

标签: android android-intent android-emulator

我试图通过从Telnet传递值来获得lat和long位置。在我完成之后,我尝试实现反向地理编码以获取模拟器中的地点的apporx地址。 Followd this link

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

log cat ..

中的错误
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();            
        }

    }

}

GeoCoder.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);

        }
    }*

GeoCodeResult

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();

    }

}

问题是地理编码结果为空

我错过了什么。指出我是我错了。谢谢

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

有时它会发生。它给我们null.Same问题在我的情况下。 在其他设备上尝试或在一段时间后尝试。尝试只在一分钟内点击一次服务器地址。 一分钟内有多个地址请求可能会导致“空”