融合位置提供程序在android marshmallow中工作正常,但无法安装它下面的版本。代码,build.gradle,清单和构建错误如下。请看看。
我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lblLocation = (TextView) findViewById(R.id.lblLocation);
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
btnStartLocationUpdates = (Button) findViewById(R.id.btnLocationUpdates);
// Check for permission
if (ContextCompat.checkSelfPermission(MainActivity.this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this, // Activity
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_FINE_LOCATION);
}
if (checkPlayServices()) {
Log.i("playservice", checkPlayServices()+"");
// Building the GoogleApi client
buildGoogleApiClient();
}
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayLocation();
}
});
}
private void displayLocation() {
mLastLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
lblLocation.setText(latitude + ", " + longitude);
} else {
lblLocation.setText("(Couldn't get the location. Make sure location is enabled on the device)");
}
}
protected synchronized void buildGoogleApiClient() {
Log.i(TAG, "Building GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Toast.makeText(getApplicationContext(),
"This device is not supported.", Toast.LENGTH_LONG)
.show();
finish();
}
return false;
}
return true;
}
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "GoogleApiClient connected!");
createLocationRequest();
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Log.i(TAG, " Location: " + mLastLocation);
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, new LocationCallback() {
@Override
public void onLocationResult(final LocationResult locationResult) {
Log.i("onLocationResult",locationResult + "");
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lblLocation.setText(locationResult.getLocations() + "");
}
});
}
@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
Log.i("onLocationAvailability", "onLocationAvailability: isLocationAvailable = " + locationAvailability.isLocationAvailable());
}
}, null);
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
+ result.getErrorCode());
}
@Override
protected void onStart() {
super.onStart();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
Log.i(TAG, "mGoogleApiClient.connect()");
}
}
的build.gradle(模块:应用) PS。我已经使用构建版本21.1.2进行了测试,但没有运气
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.bbeck.bbeck.fuseapinew"
minSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.+'
compile 'com.google.android.gms:play-services:11.0.4'
}
的AndroidManifest.xml
以下是我使用的权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
错误日志:
08-07 15:00:28.901: E/AndroidRuntime(14987): FATAL EXCEPTION: main
08-07 15:00:28.901: E/AndroidRuntime(14987): Process: com.test.test.fuseapinew, PID: 14987
08-07 15:00:28.901: E/AndroidRuntime(14987): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.test.fuseapinew/com.test.test.fuseapinew.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable/abc_vector_test.xml from drawable resource ID #0x7f020052
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.app.ActivityThread.access$900(ActivityThread.java:172)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.os.Handler.dispatchMessage(Handler.java:102)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.os.Looper.loop(Looper.java:146)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.app.ActivityThread.main(ActivityThread.java:5653)
08-07 15:00:28.901: E/AndroidRuntime(14987): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 15:00:28.901: E/AndroidRuntime(14987): at java.lang.reflect.Method.invoke(Method.java:515)
08-07 15:00:28.901: E/AndroidRuntime(14987): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
08-07 15:00:28.901: E/AndroidRuntime(14987): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
08-07 15:00:28.901: E/AndroidRuntime(14987): at dalvik.system.NativeStart.main(Native Method)
08-07 15:00:28.901: E/AndroidRuntime(14987): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/abc_vector_test.xml from drawable resource ID #0x7f020052
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.content.res.Resources.loadDrawable(Resources.java:3376)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.content.res.Resources.getDrawable(Resources.java:1872)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:374)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:711)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:195)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:81)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:131)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:205)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
08-07 15:00:28.901: E/AndroidRuntime(14987): at com.test.test.fuseapinew.MainActivity.onCreate(MainActivity.java:58)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.app.Activity.performCreate(Activity.java:5541)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
08-07 15:00:28.901: E/AndroidRuntime(14987): ... 11 more
08-07 15:00:28.901: E/AndroidRuntime(14987): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid drawable tag vector
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:986)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:930)
08-07 15:00:28.901: E/AndroidRuntime(14987): at android.content.res.Resources.loadDrawable(Resources.java:3372)