- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用 com.google.android.gms:play-services-maps:7.5.0
版本的 Google map 服务。当试图调用下面我得到 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback) ' 在空对象引用上
。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this); //error
return inflater.inflate(R.layout.fragment_example_map, container, false);
}
fragment_example_map.xml 文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.ExampleMap">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
最佳答案
我在带有 fragment 的 map 中的评论,首先你应该引用你的观点,因为你会从中调用一些东西,这就是为什么我首先夸大我的观点View view = inflater.inflate(R.layout.activity_maps, null, false);
第二次调用子 fragment 管理器this.getChildFragmentManager()
代替getActivity().getSupportFragmentManager()
这是查看 map 的完整示例
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class ClinicFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
public static ClinicFragment newInstance() {
ClinicFragment fragment = new ClinicFragment();
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_maps, null, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
需要的权限
<permission
android:name="your.package.name.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="your.package.name.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
加特征元素
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
最重要的谷歌地图键
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_key_here" />
更新如果您遇到 Error inflating class fragment,请添加此元数据
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="your_key_here" />
mainfest 的夏天
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name" >
// permission here
<application
android:name=".Activities.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
// feature element
// maps key
</application>
</manifest>
activity_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.imaadv.leaynik.ClinicFragment" />
关于android - mapFragment.getMapAsync(this) - NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30991087/
我有一个使用改造连接到 api 并在 recyclerview 中显示数据的应用程序,我现在添加了另一个 fragment ,在谷歌地图 fragment 上显示此数据。 当我在 recyclervi
我在使用 getMapAsync() 时遇到使用回调的错误,因为 getMap() 已弃用。关于如何更新此代码以免引发错误的任何想法?引入 API 以在 map 上定位图钉 05-26 22:50:1
我在尝试调用它时遇到错误“无法解析符号 getMapAsync”。我读过其他帖子,但它们的代码也和我的一样。所以这里是: package barsoftware.suedtirolpointer; i
如有任何语法错误,请原谅。我按照教程在 fragment 中查看了谷歌地图,但出了点问题。 这是我的文件 .java,它与 fragment Hook : public class MapFragme
如果我故意断开设备与互联网的连接或输入错误的 API key ,则调用 SupportMapFragment.getMapAsync GoogleMaps 只会显示灰屏 并且似乎没有办法从 Googl
我正在尝试创建一个具有简单用户界面的应用程序。它由一个 Activity 组成,其中包含 2 个按钮、一个 TextView 和一个占据一半屏幕的谷歌地图 fragment 。我使用 Android
我正在印度尼西亚 java 上做一个简单的 map View 。根据 logcat,我目前在处理 map fragment 时遇到问题,特别是在 getMapASync 返回空指针引用时。 这是我的代
我正在使用带有不同 fragment 的滑动标签。在 map fragment 中,我不能将嵌套 fragment 用作 XML。下面是 Java 代码和 XML 文件。我被困在 getMapAsyn
在今天更新我的 SDK 之前,我有工作代码(为了将来引用,请查看问题询问日期)。 .getMap 曾经发出警告,表明它已被弃用,但现在它甚至不被识别为有效输入。我假设这是因为 API 24(Andro
需要在fragment中获取 map 管理。 我的代码 public class Fragment1 extends android.support.v4.app.Fragment implement
在我将 getMap() 更改为 getMapAsync() 并实现 OnMapReadyCallback 后,我收到错误 NullPointerException。 这里是 logcat Cause
我是一名学生程序员,目前正在编写路线指南应用程序。 突然,在尝试打开我的 Android 应用程序时,它崩溃了。因此,我尝试搜索大量有关如何修复收到的错误消息的教程。不幸的是,我已经失去了希望,来到这
mapFragment.getMapAsync(this) 在 map 中返回 null 我正在尝试设计一个带有谷歌地图和抽屉导航的应用程序,我不断收到尝试在空对象引用上调用虚拟方法“void com
我在 Fragment 中实现 Google map 时遇到了问题。 这是我的 fragment 类的一部分: public class FragmentStoreFinderMap extends
我正在使用 com.google.android.gms:play-services-maps:7.5.0 版本的 Google map 服务。当试图调用下面我得到 java.lang.NullPoi
我确实需要帮助来解决我不知道如何解决的问题。我正在尝试为我父亲申请,我将与 Map 合作。该问题具体出现在 while 循环内的 mapFrag.getMapAsync() 中。 @Override
我正在尝试重振我在 2014 年制作的旧 Google map 应用程序。除了一个问题之外,没有任何问题:尝试在我在同一个类中调用的自定义方法内初始化 map ,我收到此错误: java.lang.N
我从来没有尝试过谷歌地图。这是他们使用的客户项目 compile project(':google-play-services_lib') 我必须改进他们的设计以支持最新版本的安卓 compile '
我一直在尝试将我的 SupportMapFragment 嵌套到另一个 Fragment 中,但在尝试调用 getMapAsync() 时我一直收到 nullPointerException我的 Su
我正在尝试根据 Stack Overflow 的其中一个答案创建一个交互式信息窗口。但是,我在代码中使用 getMap() 时遇到错误。虽然我已经尝试使用 getMapAsync(),但我无法解决问题
我是一名优秀的程序员,十分优秀!