当我运行我的安卓应用程序时,我在 Logcat 中遇到了这个问题。有谁知道这个问题以及如何解决它?
该应用程序没有崩溃,但是当我尝试在Google地图的自动完成搜索栏中输入某些内容时,它会关闭该活动。 并回到以前的一个,它在谷歌地图上显示当前位置。
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
这是行不通的,这个问题实际上已经被谷歌修复了。
人造人
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.project_seraphim_disease_tracker"
minSdkVersion 29
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
依赖
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.2.2'
implementation 'androidx.navigation:navigation-ui:2.2.2'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//new implementation
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.squareup.okhttp3:okhttp:4.1.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.google.android.libraries.places:places:2.2.0'
}
//google map required variables
private static final String TAG = "MapActivity";
private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
private static final String COURSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
private static final float ZOOMING_DEFAULT = 18.0f;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 9999;
private SupportMapFragment MysupportMapFragment;
private FusedLocationProviderClient MyfusedLocationProviderClient;
private GoogleMap MyMap;
private static final int AUTOCOMPLETE_REQUEST_CODE = 200;
//local variables
private Boolean MyLocationPermissionsGranted = false;
private Location Mylocation;
private LatLng MylatLng;
private LatLng SearchedLocationlatLng;
private List<Place.Field> fieldList;
private String Searchedlocation = "";
private Place AutocompletedPlaces;
//widgets
private EditText editTextsearchlocation;
这是"编辑文本"按钮
editTextsearchlocation = findViewById(R.id.edittext_SearchBar);
editTextsearchlocation.setFocusable(false);
editTextsearchlocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: initialize place field list");
fieldList = Arrays.asList(Place.Field.ADDRESS,Place.Field.LAT_LNG,Place.Field.NAME);
Log.d(TAG, "onClick: initialize place autoComplete");
Intent searchPlaceIntent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.OVERLAY, fieldList).build(Map.this);
startActivityForResult(searchPlaceIntent,100);
}
});
这是onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: currently at onActivityResult");
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
Log.d(TAG, "onActivityResult: Correct Request Code");
if (resultCode == AutocompleteActivity.RESULT_OK) {
Log.d(TAG, "onActivityResult: successfully Obtaining Places");
AutocompletedPlaces = Autocomplete.getPlaceFromIntent(data);
Log.i(TAG, "Place: "
+ AutocompletedPlaces.getName() + ", "
+ AutocompletedPlaces.getId());
SearchedLocationlatLng = AutocompletedPlaces.getLatLng();
Searchedlocation = AutocompletedPlaces.getAddress();
editTextsearchlocation.setText(Searchedlocation);
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
Log.d(TAG, "onActivityResult: Failed To Obtaining Places");
Status status = Autocomplete.getStatusFromIntent(data);
Log.i(TAG, status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
} else if (resultCode == RESULT_FIRST_USER){
}
}
}
我可以在日志中看到这一点
"onActivityResult: currently at onActivityResult"
在日志中的这一行之后是
E/libprocessgroup: set_timerslack_ns write failed: Operation not permitted
找到了解决方案。
当我尝试初始化地点时,我使用了存储在res/values/Strings中的API_KEY。 我所做的只是将实际的 api 密钥放在代码中。
从
Places.initialize(getApplicationContext(),"@string/API_KEY");
自
Places.initialize(getApplicationContext(),YOUR API KEY);
这也发生在我身上!我发现这是因为我的 API 没有启用(谷歌上的地方(。它也适用于以下内容!:)
Places.initialize(getApplicationContext(),"@string/API_KEY");
上层解决方案对我不起作用,因为即使应用程序没有运行,我仍然会收到消息,这意味着它与其他内容有关。我不得不删除 AVD 并重新创建它。它解决了问题。