我在尝试更新大于22的compileSdkVersion andTargetSdkversion后出现此错误。好像我在doInBackground方法中执行任何GUI任务但我无法弄明白。我非常感谢解决方案。
这是我的堆栈跟踪:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: xy.abc.xyz.xy, PID: 15246
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at android.net.wifi.IWifiManager$Stub$Proxy.getScanResults(IWifiManager.java:1066) at android.net.wifi.WifiManager.getScanResults(WifiManager.java:1311)
at xy.abc.xyz.xy.config.WlanInfoAdapter.getNewItems(WlanInfoAdapter.java:72)
at xy.abc.xyz.xy.android.data.ChangeableArrayAdapter$1.doInBackground(ChangeableArrayAdapter.java:78)
at xy.abc.xyz.xy.android.data.ChangeableArrayAdapter$1.doInBackground(ChangeableArrayAdapter.java:74)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
WlanInfoAdapter.java:72类中的方法在这里:
@Override
protected List<WlanInfo> getNewItems() {
WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
List<ScanResult> scanResults = wifiManager.getScanResults();
List<WlanInfo> infoList = new ArrayList<>(scanResults.size());
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
try {
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
Map<String, WifiConfiguration> configuredNetworkIds = new HashMap<>();
for (WifiConfiguration configuredNetwork : configuredNetworks) {
if (configuredNetwork.SSID != null) {
configuredNetworkIds.put(WlanInfo.cleanSSID(configuredNetwork.SSID), configuredNetwork);
} else {
wifiManager.removeNetwork(configuredNetwork.networkId);
}
}
for (ScanResult scanResult : scanResults) {
WlanInfo wlanInfo = new WlanInfo(scanResult);
WifiConfiguration configuration = configuredNetworkIds.get(wlanInfo.getSSID());
wlanInfo.setConfiguration(configuration);
wlanInfo.setIsActive(configuration != null && configuration.networkId == connectionInfo.getNetworkId());
if (showUnknown || wlanInfo.isConfigured() || wlanInfo.isOpen()) {
infoList.add(wlanInfo);
}
}
}
catch (Exception e) { e.printStackTrace(); return null; }
return infoList;
}
下面给出了显示错误的changeableArrayAdapter.java类中的方法。它调用getNewItems()方法的地方: ChangeableArrayAdapter.java:78 ChangeableArrayAdapter.java:74
public void triggerRefresh() {
AsyncTask<Void, Void, List<T>> asyncTask = new AsyncTask<Void, Void, List<T>>() {
@Override
protected List<T> doInBackground(Void... objects) {
return getNewItems();
//return null;
}
答案 0 :(得分:0)
您需要添加ACCESS_COARSE_LOCATION
或ACCESS_FINE_LOCATION
作为Android清单文件的权限。
答案 1 :(得分:0)
如果您只定位API 22,那么您需要在 android清单文件中添加权限
facebook
如果您的目标是 Android Marshmallow(Api 23及以上),则需要在您的应用中授予运行时权限。
答案 2 :(得分:0)
在 String fileDirectory = "/sys/some/path/to/some/";
String fileName = "file";
try
{
File file = new File(fileDirectory, fileName );
if (file.Exists()) {
// do something
}
else
{
// do something else
}
}
catch (IOException e)
{
}
代码
AndroidManifest.xml
application
此外,对于Android版&gt; = 23,您需要在运行时
中请求权限<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
和
if (ContextCompat.checkSelfPermission(YourActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&&
ContextCompat.checkSelfPermission(YourActivity.this,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
askForLocationPermissions();
} else {
//do your work
}
另外
private void askForLocationPermissions() {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
new android.support.v7.app.AlertDialog.Builder(this)
.setTitle("Location permessions needed")
.setMessage("you need to allow this permission!")
.setPositiveButton("Sure", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(YourActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
}
})
.setNegativeButton("Not now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// //Do nothing
}
})
.show();
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
// MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
和
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE:
if (isPermissionGranted(permissions, grantResults, Manifest.permission.ACCESS_FINE_LOCATION)) {
//Do you work
} else {
Toast.makeText(this, "Can not proceed! i need permission" , Toast.LENGTH_SHORT).show();
}
break;
}
}