如何在android studio中解决“错误:找不到符号变量new_location”?

问题描述 投票:1回答:1

我正在尝试执行这里找到的基本教程: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地图,在打开应用程序时显示相同的位置/区域(即特定的经度和纬度)。

以下是我的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地图上显示我的笔记本电脑设备的当前位置。但是,我无法在当前状态的虚拟设备上运行该应用程序。

android android-gradle mapbox
1个回答
1
投票

您遇到错误,因为您在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个新的字符串,其名称与活动引用的完全相同

<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>
© www.soinside.com 2019 - 2024. All rights reserved.