gpt4 book ai didi

android - 位置管理器 : java. lang.NoSuchMethodError

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:25 30 4
gpt4 key购买 nike

我是这个社区的新人,但我已经习惯了从你那里学到很多东西!实际上我开始在 Android Studio 上编程,但现在我卡在了某个地方。我相信这很简单,您很快就会发现。

在 Emulator Nexus 5 API 23(已安装在 Android Studio 中的那个)中,它正在运行,但在我的移动 Android 5.1.1 API 22 上它无法启动。

错误:09-20 19:15:11.526 17776-17776/com.example.stefano.aiutomamma01 E/AndroidRuntime:致命异常:main
进程:com.example.stefano.aiutomamma01,PID:17776
java.lang.NoSuchMethodError: 没有虚拟方法 checkSelfPermission(Ljava/lang/String;)I in class Lcom/example/stefano/aiutomamma01/MainActivity;或其父类(super class)('com.example.stefano.aiutomamma01.MainActivity' 的声明出现在/data/app/com.example.stefano.aiutomamma01-2/base.apk 中)
在 com.example.stefano.aiutomamma01.MainActivity.onCreate(MainActivity.java:28)
在 android.app.Activity.performCreate(Activity.java:5990)
在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
在 android.app.ActivityThread.access$800(ActivityThread.java:156)
在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
在 android.os.Handler.dispatchMessage(Handler.java:102)
在 android.os.Looper.loop(Looper.java:211)
在 android.app.ActivityThread.main(ActivityThread.java:5373)
在 java.lang.reflect.Method.invoke( native 方法)
在 java.lang.reflect.Method.invoke(Method.java:372)
在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

Java代码:

package com.example.stefano.aiutomamma01;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements LocationListener {

//GPS
private LocationManager locationManager;

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

//GPS
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//CATCHED
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// public void requestPermissions(@NonNull String[] permissions, int requestCode)
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}


//GPS
@Override
public void onLocationChanged(Location location) {

String str = "Latitude: " + location.getLatitude() + "Longitude: " + location.getLongitude();

Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();

}

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

}

//GPS
@Override
public void onProviderEnabled(String provider) {

}

//GPS
@Override
public void onProviderDisabled(String provider) {

}

}

抱歉发错帖子。顺便说一句:我无法在网上找到解决方案...

问候

最佳答案

运行时权限在 API 23 中引入,与用于处理权限的方法相同。使用 ActivityCompat类方法以向后兼容的方式处理这个问题。

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
...
}

等等。

关于android - 位置管理器 : java. lang.NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32682306/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com