小编Fe *_* Ri的帖子

mSecurityInputMethodService在logcat中为null

我写了一个小的Android应用程序,应该显示智能手机的当前位置(最后的已知位置).虽然我复制了示例代码,并尝试了其他几种解决方案,但每次都会出现相同的错误.我的应用程序包含一个按钮.按下按钮应记录经度和纬度,但仅记录日志"mSecurityInputMethodService is null".

这是MainActivity.java:

public class MainActivity extends Activity {
int response;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onbutclick();
        }
    });

}
public void onbutclick(){
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
        Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(loc != null){
            Log.d("Latitude",Double.toString(loc.getLatitude()));
            Log.d("Longitude",Double.toString(loc.getLongitude()));
        }

    }else{
        ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.ACCESS_FINE_LOCATION},response);
        Intent inte = getIntent();
        finish();
        startActivity(inte);
    }
}
Run Code Online (Sandbox Code Playgroud)

我也在<uses-permission> …

java null gps android location

13
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

gps ×1

java ×1

location ×1

null ×1