百度地图APi 定位后UI无法刷新 这是怎么回事 2C

Toast已经提示定位成功,但是TextView没更新
按下home键后台应用后再进来UI才会刷新,这是怎么回事,要怎么解决

3个回答

上面的图已经定位成功,HelloWorld却没刷新
下面的图是 要手机按一下home键再进来UI才会刷新
这是怎么回事

package com.example.lbstest2;


import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    public LocationClient mLocationClient;
    private TextView positionText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLocationClient = new LocationClient(getApplicationContext());
        setContentView(R.layout.activity_main);
        mLocationClient.registerLocationListener(new MyLocationListener());
        positionText = (TextView) findViewById(R.id.position_text_view);
        List<String> permissionList = new ArrayList<>();
        if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
        if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
        {
            permissionList.add(Manifest.permission.READ_PHONE_STATE);
        }
        if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
        {
            permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if(!permissionList.isEmpty())
        {
            String[] permissions = permissionList.toArray(new String [permissionList.size()]);
            ActivityCompat.requestPermissions(MainActivity.this,permissions,1);
        }else
        {
            requestLocation();
        }
    }
    private void requestLocation()
    {
        initLocation();
        mLocationClient.start();
    }
    private void initLocation()
    {
        LocationClientOption option = new LocationClientOption();
        option.setScanSpan(5000);
        option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);
        mLocationClient.setLocOption(option);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLocationClient.stop();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode)
        {
            case 1:
                if(grantResults.length > 0)
                {
                    for(int result : grantResults)
                    {
                        if(result != PackageManager.PERMISSION_GRANTED)
                        {
                            Toast.makeText(this,"没有权限将无法使用本程序", Toast.LENGTH_SHORT).show();
                        }
                        finish();
                        return;
                    }
                    requestLocation();
                }else
                {
                    Toast.makeText(this,"发生未知错误,请尝试重新安装本程序",Toast.LENGTH_SHORT).show();
                    finish();
                }
            default:
                break;
        }
    }
    public class MyLocationListener implements BDLocationListener
    {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            StringBuilder currentPosition = new StringBuilder();
            try {
                currentPosition.append("纬度:").append(bdLocation.getLatitude()).append("\n");
                currentPosition.append("经度:").append(bdLocation.getLongitude()).append("\n");
                currentPosition.append("定位方式:");
                if (bdLocation.getLocType() == BDLocation.TypeGpsLocation) {
                    currentPosition.append("GPS");
                } else if (bdLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
                    currentPosition.append("网络");
                } else {
                    currentPosition.append("未知错误");
                }
                positionText.setText(currentPosition);
                Toast.makeText(MainActivity.this,"定位成功\n"+currentPosition,Toast.LENGTH_SHORT).show();
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {

        }
    }


}



     public class MyLocationListener implements BDLocationListener
    {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            StringBuilder currentPosition = new StringBuilder();
            try {
                currentPosition.append("纬度:").append(bdLocation.getLatitude()).append("\n");
                currentPosition.append("经度:").append(bdLocation.getLongitude()).append("\n");
                currentPosition.append("定位方式:");
                if (bdLocation.getLocType() == BDLocation.TypeGpsLocation) {
                    currentPosition.append("GPS");
                } else if (bdLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
                    currentPosition.append("网络");
                } else {
                    currentPosition.append("未知错误");
                }
                positionText.setText(currentPosition);
                Toast.makeText(MainActivity.this,"定位成功\n"+currentPosition,Toast.LENGTH_SHORT).show();
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }

这里是显示定位的 Toast 上面的 positionText.setText(currentPosition);更新UI没用 要手机按一下home键再进来UI才会刷新

用Handler应该可以

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问