【问题标题】:Using Google Places API, Location class throws Null Pointer Exception when used使用 Google Places API,Location 类在使用时抛出空指针异常
【发布时间】:2013-12-29 09:40:41
【问题描述】:

我希望程序能够使用 Location location.getLatitude()/Location location.getLongitude 方法返回上述 a 和 b 值的值,但每当我用返回双精度值的方法替换 a 和 b 时getLongitude 和 getLatitude 提供的值或实际的 get 函数本身,我得到一个错误。

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


}

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

已编辑:代码

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


            }
        });

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:

    如果您从location.getLatitude()location.getLongitude() 获得NullPointerException,则表明location 为空。

    您还没有显示location 的来源,或者您已经看到方法调用返回ab,但基本上这就是原因。也许您有一个局部变量,在某处隐藏了一个实例变量?例如:

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

    【讨论】:

    • 我尝试使用以下方法解决它: Location location = new Location();但它希望新对象有一个参数,我认为我不需要。我的位置位置是全局的,在主类的开头定义。
    • @user1204072:但它在哪里被赋值?在没有看到任何相关代码的情况下,很难非常帮助您。
    • @user1204072:您仍然没有显示任何为location 变量赋值的内容。如果你真的没有任何东西分配给那个变量,那么它当然仍然是 null...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 2020-11-22
    • 2020-08-10
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多