android - 如何在android studio中解析 "error: cannot find symbol variable new_location"?

标签 android android-gradle-plugin mapbox

我正在尝试执行此处找到的基本教程:https://docs.mapbox.com/help/tutorials/android-location-listening/

我已经能够复制代码并将我的 accessToken 插入适当的位置。我的 .xml 文件或 gradle 脚本中没有任何错误。但是,在 MainActivity 中出现以下错误:

error: cannot find symbol variable user_location_permission_explanation
error: cannot find symbol variable user_location_permission_not_granted
error: cannot find symbol variable new_location

我已经能够在打开应用程序时生成显示相同位置/区域(即特定经度和纬度)的基本 Mapbox map 。

以下是我的MainActivity的导入

import android.annotation.SuppressLint;
import android.location.Location;
import android.util.Log;
import android.widget.Toast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.annotation.NonNull;
// Classes needed to initialize the map
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
// Classes needed to handle location permissions
import com.mapbox.android.core.permissions.PermissionsListener;
import com.mapbox.android.core.permissions.PermissionsManager;
import java.util.List;
// Classes needed to add the location engine
import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEngineCallback;
import com.mapbox.android.core.location.LocationEngineProvider;
import com.mapbox.android.core.location.LocationEngineRequest;
import com.mapbox.android.core.location.LocationEngineResult;
import java.lang.ref.WeakReference;
// Classes needed to add the location component
import com.mapbox.mapboxsdk.location.LocationComponent;
import com.mapbox.mapboxsdk.location.LocationComponentActivationOptions;
import com.mapbox.mapboxsdk.location.modes.CameraMode;
import com.mapbox.mapboxsdk.location.modes.RenderMode;

错误的问题在以下位置:

 @Override
    public void onExplanationNeeded(List<String> permissionsToExplain) {
        Toast.makeText(this, R.string.user_location_permission_explanation, Toast.LENGTH_LONG).show();
    }
@Override
public void onPermissionResult(boolean granted) {
    if (granted) {
        if (mapboxMap.getStyle() != null) {
            enableLocationComponent(mapboxMap.getStyle());
        }
    } else {
        Toast.makeText(this, R.string.user_location_permission_not_granted, Toast.LENGTH_LONG).show();
        finish();
    }
}
  // Create a Toast which displays the new location's coordinates
                Toast.makeText(activity, String.format(activity.getString(R.string.new_location),
                        String.valueOf(result.getLastLocation().getLatitude()), String.valueOf(result.getLastLocation().getLongitude())),
                        Toast.LENGTH_SHORT).show();

以下是我的activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <com.mapbox.mapboxsdk.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        mapbox:layout_constraintBottom_toBottomOf="parent"
        mapbox:layout_constraintEnd_toEndOf="parent"
        mapbox:layout_constraintStart_toStartOf="parent"
        mapbox:layout_constraintTop_toTopOf="parent"
        mapbox:mapbox_cameraTargetLat="36.16266"
        mapbox:mapbox_cameraTargetLng="-86.78160"
        mapbox:mapbox_cameraZoom="12"/>

</android.support.constraint.ConstraintLayout>

我的项目build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我的模块build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:7.3.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

我希望该应用程序能够在 Mapbox map 上显示我的笔记本电脑设备的当前位置。但是,我无法在当前状态下的虚拟设备上运行该应用程序。

最佳答案

您遇到错误是因为您在 strings.xml 中没有任何名为“user_location_permission_explanation”、“user_location_permission_not_granted”或“new_location”的字符串
Image of project side bar

解决方案 1.
只需将“R.string.user_location_permission_not_granted”替换为您自己的字符串,例如:

Toast.makeText(this, "Location permission granted",  Toast.LENGTH_LONG).show();

Toast.makeText(this, "Location not granted", Toast.LENGTH_LONG).show();       

Toast.makeText(activity, String.format("New Location",
                    String.valueOf(result.getLastLocation().getLatitude()), String.valueOf(result.getLastLocation().getLongitude())),
                    Toast.LENGTH_SHORT).show();

解决方案 2.
双击 strings.xml 文件并添加 3 个新字符串,名称与 Activity 引用它们的名称完全相同

<resources>
    <string name="app_name">MapboxApp</string>
    <string name="user_location_permission_explanation">Permission granted</string>
    <string name="user_location_permission_not_granted">Permission not granted</string>
    <string name="new_location">New Location</string>
</resources>

关于android - 如何在android studio中解析 "error: cannot find symbol variable new_location"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55669367/

相关文章:

android - Admob Interstitial 广告(全屏)不会显示

android - 在android- ListView 中仅找出完全可见的列表项

android - 使用 Gradle 的 Travis-CI Android 测试不断超时

java - 圆角mapbox mapView

android - BouncyCaSTLe 版本和 SSL 证书验收

android - 没有阴影的自定义标题栏

android - 如何将 BuildConfig 值传递给依赖模块?

android - 当我对其应用 crashlytics 时应用程序崩溃

ios - 如何在没有可见 map 的情况下查询 Mapbox MGLMapView 功能?

php - 从 MySql 在 php 中创建 GeoJson 以与 MapBox javascript API 一起使用