【问题标题】:Unable to update TextView from location update无法从位置更新中更新 TextView
【发布时间】:2019-12-17 19:22:20
【问题描述】:

我只是获取设备的当前位置并显示在 TextView 上,但 TextView 不显示结果,但是,Java 代码似乎正确地获取了位置,但没有像我尝试过的那样更新 TextView很多相关代码没有位置获取错误。 此外,我使用物理设备来运行应用程序,而不是模拟器。 我最近尝试过的代码如下:

public class roadSideSelection extends AppCompatActivity implements AdapterView.OnItemSelectedListener, LocationListener {

    int MY_PERMISSION_LOCATION=100;
    int MY_PERMISSION_ACCESS_COARSE_LOCATION=100;
    protected LocationManager locationManager;
    protected Context context;
    TextView txtLat;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_road_side_selection);

        marshmallowGPSPremissionCheck();

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSION_ACCESS_COARSE_LOCATION);
        }

        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

        txtLat = findViewById(R.id.latitudeTextview);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    }

    @Override
    public void onLocationChanged(Location location) {
        txtLat = findViewById(R.id.latitudeTextview);
        String s = getString(R.string.latitude, location.getLatitude(), location.getLongitude());
        txtLat.setText(s);
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("Latitude","disable");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("Latitude","enable");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Latitude","status");
    }

    private void marshmallowGPSPremissionCheck() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && checkSelfPermission(
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && checkSelfPermission(
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSION_LOCATION);
        } else {
            //   gps functions.
               Toast.makeText(this, "Permission Denied!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @Nullable String[] permissions,@Nullable int[] grantResults) {
        if (requestCode == MY_PERMISSION_LOCATION
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "Permission Granted!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Permission Denied!", Toast.LENGTH_SHORT).show();
        }
    }
}
'''

Strings.xml

纬度:%1$d;经度:%2$d


【问题讨论】:

  • 日志中有任何错误?
  • 也在 string.xml 中分享你的字符串声明
  • @Jacks logcat strings.xml 中没有错误:Latitude: %1$d;经度:%2$d

标签: java android textview geolocation


【解决方案1】:
txtLat = (TextView) findViewById(R.id.latitudeTextview);

将视图投射到 TextView 并确保使用正确的视图 ID

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多