使用Google Places API时,Location类会在使用时抛出Null Pointer Exception

时间:2022-04-30 15:51:45

I would like to have the program be able to use Location location.getLatitude()/Location location.getLongitude method return values for a and b values above, but whenever I replace a and b with either a method that returns the double value that getLongitude and getLatitude provide or the actual get functions themselves, I get an error.

我想让程序能够使用Location location.getLatitude()/ Location location.getLongitude方法返回上面a和b值的值,但每当我用一个返回getLongitude的double值的方法替换a和b时和getLatitude本身提供或实际获取函数,我收到一个错误。

public void onClick(View v) {

    requesturl = "https://maps.googleapis.com/maps/api/place/search/json?" + 
    "location=" + a + "," + b + "&radius=6000&" + "types=bank&sensor=false&key=AIzaSyDXNlHRDZnWW0T0tvBUjpyA8k2K9sjS2cM";    

    HHand(requesturl);


}

How could I go about making it so that a and b can get their values from getLatitude() and getLongitude() functions?

我怎样才能使a和b从getLatitude()和getLongitude()函数中获取它们的值?

Edited: Code

public class Freedom extends ActivityGroup implements LocationListener {
/** Called when the activity is first created. 
 * @return */

String keystring="";
String requesturl;
LocationManager locman;
Location location;

//Awesome      
public String convertStreamToString(InputStream is) { 
    return new Scanner(is).useDelimiter("\\A").next();
} 



public void HHand(String a) {


    //HTTP Request Processing for URL
       HttpClient client=new DefaultHttpClient();
       StringBuilder builder=new StringBuilder(a);
       builder.append(URLEncoder.encode(keystring));
       HttpPost post=new HttpPost(a);

       try {
           org.apache.http.HttpResponse response=client.execute(post);
           HttpEntity entity=response.getEntity();


           if (entity != null) {
               InputStream is = entity.getContent();
               String val = convertStreamToString(is);
               Log.e("", val);

              }


           }


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

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


}


@Override
public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    locman = (LocationManager) this.getSystemService(LOCATION_SERVICE);



}



protected void onResume() {
    super.onResume();
    locman.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);
  }


public boolean onCreateOptionsMenu(Menu menu)  {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.btmmnu, menu);
    return true;
}

protected Dialog onCreateDialog(int id) {

        final Dialog dialog = new Dialog(this);
        OnClickListener txtlstn = new OnClickListener() {
            public void onClick(View v) {
                dismissDialog(0);
            }
        };

        switch(id) {
        case 0:                         

            ...
        }
        return dialog;
}




public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.About:                
        showDialog(0);
        return true;
    case R.id.Guidance:

         final Button Travel = (Button) findViewById(R.id.travel);  
         Travel.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                requesturl = "https://maps.googleapis.com/maps/api/place/search/json?" + 
                          "location=" + location.getLatitude() + "," + location.getLongitude() + "&radius=6000&" +
                          "types=car_rental&sensor=false&key=AIzaSyDXNlHRDZnWW0T0tvBUjpyA8k2K9sjS2cM";  
                        HHand(requesturl);


            }
        });

1 个解决方案

#1


0  

If you're getting a NullPointerException from location.getLatitude() or location.getLongitude(), that would suggest that location is null.

如果您从location.getLatitude()或location.getLongitude()获得NullPointerException,则表明该位置为null。

You haven't shown where location is meant to be coming from, or where you've seen that the method calls return a and b, but that will be the cause, basically. Perhaps you've got a local variable hiding an instance variable somewhere? For example:

你没有显示位置来自哪里,或者你已经看到方法调用返回a和b的位置,但这基本上就是原因。也许你有一个隐藏实例变量的局部变量?例如:

public class Foo
{
    private Location location;

    public Foo(double latitude, double longitude)
    {
        // Careful - this declares a new *local* variable, so it's not
        // changing the value of this.location
        Location location = new Location();
        location.setLatitude(latitude);
        location.setLongitude(longitude);
        ...
    }

    public void bang()
    {
        // This will throw, as location is still null
        double x = location.getLatitude();
    }
}

#1


0  

If you're getting a NullPointerException from location.getLatitude() or location.getLongitude(), that would suggest that location is null.

如果您从location.getLatitude()或location.getLongitude()获得NullPointerException,则表明该位置为null。

You haven't shown where location is meant to be coming from, or where you've seen that the method calls return a and b, but that will be the cause, basically. Perhaps you've got a local variable hiding an instance variable somewhere? For example:

你没有显示位置来自哪里,或者你已经看到方法调用返回a和b的位置,但这基本上就是原因。也许你有一个隐藏实例变量的局部变量?例如:

public class Foo
{
    private Location location;

    public Foo(double latitude, double longitude)
    {
        // Careful - this declares a new *local* variable, so it's not
        // changing the value of this.location
        Location location = new Location();
        location.setLatitude(latitude);
        location.setLongitude(longitude);
        ...
    }

    public void bang()
    {
        // This will throw, as location is still null
        double x = location.getLatitude();
    }
}