簡體   English   中英

通過Gps獲取位置信息不起作用

[英]Get location by Gps not working

我有這個錯誤。

02-13 15:13:18.110: E/AndroidRuntime(2256): java.lang.RuntimeException: Unable to start activity ComponentInfo{scom.example.sampol/scom.example.sampol.MainActivity}: java.lang.NullPointerException

這是我的代碼。 關於獲取位置

public void onclickLocation(View v){

            txt1=(TextView) findViewById(R.id.textLocation);
            /* Use the LocationManager class to obtain GPS locations */
             mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            LocationListener mlocListener = new MyLocationListener();
            mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

        }
        /* Class My Location Listener */
        public class MyLocationListener implements LocationListener {

            @Override
            public void onLocationChanged(Location loc){
                loc.getLatitude();
                loc.getLongitude();
                Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
                 try {
                    addresses = gcd.getFromLocation(loc.getLatitude(),loc.getLongitude(), 1);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 text=(addresses!=null)?"City : "+addresses.get(0).getSubLocality()+"\n Country : "+addresses.get(0).getCountryName():"Unknown Location";

                String textVal = "My current location is: "+ text;

                txt1.setText(textVal);

            }

            @Override
            public void onProviderDisabled(String provider){
                Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
            }

            @Override
            public void onProviderEnabled(String provider){
                Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras){
                }
        }

您是否已在清單XML中添加此權限? 清單中的訪問課程位置和訪問精細位置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM