您好,我对此还很陌生,所以请帮助我理解为什么会出现此错误。我已经尝试了很多解决方案,所以我只想列出我所做的一切,因为我似乎无法理解为什么会发生这种情况。
我创建了一个项目,该项目在最低 SDK 21 上集成了 GoogleMaps,以在 SDK 28 上进行目标/编译。我确实在宣言中调用了所需的权限。
我创建了一个扩展 MapFragment 类的文件,一切似乎都工作正常。我能够检查并请求用户位置的权限(该框确实显示),但是当我调用 onRequestPermissionResult 方法时,它显示不同,并给我一个错误,提示“错误:找不到符号类 NonNull ”:
@Override
public void onRequestPermissionsResult(int requestCode, @androidx.annotation.NonNull String[] permissions, @androidx.annotation.NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
Run Code Online (Sandbox Code Playgroud)
在我的其他片段(android.support.v4.app)类中,@androidx.annotation.NonNull 是@NonNull。我首先想到也许我需要将实现“com.android.support:support-annotations:28.0.0”添加到 build.gradle,但事实并非如此。然后,我尝试将 @androidx.annotation.NonNull 替换为 @NonNull,这使得错误消失,但每当我单击允许或拒绝时,它都不会命中 onRequestPermissionResult 方法。
我创建了一个检查权限的方法,但它不允许我在不检查构建是否大于或等于 SDK 23 的情况下单独使用 requestPermission,但我的最小 SDK 是 21。因此,我只是检查构建是否大于或等于 SDK 23。大于或等于 21 并使用 ActivityCompat 获取 requestPermission 方法并且它有效。它会检查并请求许可,所以我想也许 onRequestPermissionResult 只能在 MainActivity 中工作,这是我不想要的。我希望能够在检查 MapFragment 内是否授予请求后调用方法。是因为 android.support.v4.app 不支持 MapFragment 吗?它看起来像这样:
private boolean checkAskPermission(int requestCode, String permissionString){
if(Build.VERSION.SDK_INT >= 21){
int permission …Run Code Online (Sandbox Code Playgroud) 尽可能多的我得到以下错误:java.lang.noclassdeffounderror: com.google.android.gms.R$styleable尝试实现Android版地图时.
不知怎的,我一定错过了我做错了什么,因为它不起作用.我首先尝试将google-play-services.lib复制到lib文件夹中,但我看到某处我不应该这样做.
然后我尝试将addon-google_apis-google-8导入为项目,但该项目给出了错误 The import com.google cannot be resolved.
我正在关注Vogella教程:
活动:
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
map = fm.getMap();
//map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.btn_logo)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
Run Code Online (Sandbox Code Playgroud)
表现:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.testmap.test.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.testmap.test.permission.MAPS_RECEIVE" …Run Code Online (Sandbox Code Playgroud) maps android noclassdeffounderror mapfragment supportmapfragment
当我在运行时用SupportMapFragment替换容器时,它不会填充Parent.它仅部分占据屏幕.
HomeActivity2.java
package com.example.mapdemo;
import com.meteraid.android.services.LocationService;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
public class HomeActivity2 extends BaseActivity2 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_activity2);
LocationService locationService=LocationService.getInstance(this);
Location location=locationService.getCurrentLocation();
GoogleMapFragment mapFragment=new GoogleMapFragment();
mapFragment.setCurrentLocation(location);
android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,mapFragment);
fragmentTransaction.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_activity2, menu);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
GoogleMapFragment
package com.example.mapdemo;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import …Run Code Online (Sandbox Code Playgroud) 我的 Main Activity 中有一个地图片段,在 onMapReady 上我添加了 300 个标记。标记分为5个类别,每个类别保存在一个List中category1(2,3,4,5) = new ArrayList。我有一个按钮,可以根据警报对话框中的用户选项将标记设置为可见或不可见,从而按类别过滤标记。
for (Marker marker : category1) {
marker.setVisible(true);.....
Run Code Online (Sandbox Code Playgroud)
一切都按其应有的方式正常工作,直到屏幕配置发生变化。我发现保持可见标记旋转的一种解决方案是在我的 onCreate 中添加
if (savedInstanceState == null) {
mapFragment.setRetainInstance(true);}
Run Code Online (Sandbox Code Playgroud)
这适用于保留当前选择,但当我尝试再次过滤时没有任何变化。地图片段的状态保留为我在配置更改之前过滤的标记。
另一方面,如果我不使用 mapFragment.setRetainInstance(true); 每次配置更改时,所有标记都会再次添加到片段上,过滤再次起作用,但我每次都必须重新过滤。
如何添加所有标记一次,然后根据类别过滤器隐藏/显示它们,在屏幕旋转之间保留可见标记并从上次状态开始再次过滤它们?
更新
这是我所拥有的(我尝试仅添加相关代码)
Boolean mSetCameraPosition;
Boolean initialMarkers;
Boolean checkBox1Checked, checkBox2Checked, checkBox3Checked, checkBox4Checked, checkBox5Checked;
private int mapTypeSelected;
CheckBox cbAllDay, cbBefore12, cbBetween1216, cbBetween1620, ccbAfter20;
AlertDialog dialog;
List<Marker> firstCategoryList = new ArrayList<>();
List<Marker> secondCategoryList = new ArrayList<>();
List<Marker> thirdCategoryList = new ArrayList<>();
List<Marker> fourthCategoryList = new ArrayList<>();
List<Marker> fifthCategoryList = new ArrayList<>(); …Run Code Online (Sandbox Code Playgroud) 我想在android studio中的默认Googlemap活动之上添加一个按钮。我通过更改Googlemap片段的XML代码来尝试了此操作:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/location_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:padding="0dp"
android:paddingBottom="0dp"
android:background="#359c5e"
android:text="@string/go"
android:textColor="#ffffff" />
Run Code Online (Sandbox Code Playgroud)
但是我无法在我的地图碎片上得到Button。我通过在XML文件中的Fragment和Viceversa之前编写Button尝试了很多方法,仍然面临着同样的问题。
我的要求是
如何完成这项任务?任何建议表示赞赏。