我正在使用com.google.android.gms:play-services-maps:7.5.0
版Google地图服务。在尝试拨打以下内容时,我会java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
。
@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>
答案 0 :(得分:115)
我在带有片段的地图中的评论,首先你应该提醒你的观点,因为你会从中调用一些东西,这就是为什么我首先给我的观点充气
View view = inflater.inflate(R.layout.activity_maps, null, false);
第二次调用子片段管理器this.getChildFragmentManager()
而不是getActivity().getSupportFragmentManager()
以下是查看地图的完整示例
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" />
主要节日的总结
<?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" />
答案 1 :(得分:18)
使用 private AppCompatActivity mActivity;
public static String TAG = "TripListFragment";
public TripListFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Activity)
mActivity = (AppCompatActivity) context;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
ActionBar actionBar = mActivity.getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_trip_list, container, false);
}
@Override
public void onResume() {
super.onResume();
((HomeActivity) mActivity).setDrawerIndicatorEnabled(false);
}
代替片段中的getChildFragmentManager()
。
像:
getActivity().getSupportFragmentManager()
答案 2 :(得分:10)
您试图在片段存在之前找到它。您指示具有该片段的布局为fragment_example_map.xml
。但是,您试图在膨胀该布局文件之前找到地图片段。这不起作用。
除此之外,您似乎试图从该片段的onCreateView()
方法中的另一个片段内部获取地图片段。我不知道你为什么在这里嵌套片段,因为它似乎会使你的代码更复杂,更脆弱,没有明显的好处。
答案 3 :(得分:7)
如果您在片段中使用android:name="com.google.android.gms.maps.MapFragment"
,请使用以下代码更改您的代码:
MapFragment mapFragment1 = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
mapFragment1.getMapAsync(this);
但是,如果您使用的是android:name="com.google.android.gms.maps.SupportMapFragment"
在你的片段中然后使用这段代码:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
答案 4 :(得分:5)
这对我有用:
SELECT
*
FROM
assign_sec
WHERE assign_sec.stud_id
NOT IN (SELECT stud_id FROM attendance WHERE attendance.att_date="2018-01-03" AND attendance.att_sec = "10")
AND assign_sec.sec_id = "10"
答案 5 :(得分:3)
遇到同样的问题。由于您是在Fragment中使用Map片段,因此请使用getChildFragmentManager()而不是getActivity()。getFragmentManager()或getFragmentManager()
答案 6 :(得分:2)
根据Android版本或SDK版本5及更高版本,使用getChildFragmentManager()及其下方使用getFragmentManager()。
答案 7 :(得分:1)
使用它。
GoogleMap gooleMap;
((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
}
答案 8 :(得分:1)
getFragmentManager()可能无法正常工作。您必须使用 getChildFragmentManager()。
Hash
以下是我的xml声明
say [ [1,2], [3,4]]; # [[1 2] [3 4]]
say [|[1,2], [3,4]]; # [1 2 [3 4]]
say [|1, 2, [3,4]]; # [1 2 [3 4]]
您还需要在片段中实现OnMapReadyCallback。
答案 9 :(得分:1)
简单的答案是,如果你想添加Map Fragment this
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.earthreport.fragments.HomeFragment"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="81dp">
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@id/guideline4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/guideline3"
tools:layout="@layout/fragment_home" />
</android.support.constraint.ConstraintLayout>
在你的片段中喜欢这种布局
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Find a reference to the {@link ListView} in the layout
// Inflate the layout for this fragment
// To get the referance we don't have findviewbyId
// method in fragment so we use view
View view = inflater.inflate(R.layout.fragment_home, container, false);
SupportMapFragment mapFragment = (SupportMapFragment)
this.getChildFragmentManager()
.findFragmentById(R.id.map)
mapFragment.getMapAsync(this);
.....
然后你必须在像这个
这样膨胀片段之后添加这一行<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context="package com.example.android.map" />
</android.support.constraint.ConstraintLayout>
进入您的活动,就像这个布局一样
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
然后你必须添加这段代码。
{{1}}
如果我错了,请纠正我,但在给布局膨胀或设置内容视图后总是添加地图片段。
答案 10 :(得分:1)
我正在使用 SupportMapFragment ,并在XML中定义了 MapFragment 名称属性
android:name="com.google.android.gms.maps.MapFragment"
所以我用SupportMapFragment替换了代码,它开始正常工作
android:name="com.google.android.gms.maps.SupportMapFragment"
答案 11 :(得分:0)
替换此代码
<fragment
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="400dp" />
和
if (googleMap == null) {
mapFrag = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
}else{
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
}
答案 12 :(得分:0)
只需在您的活动中的supportMapFragment.getMapAsync(this);
中致电onResume
答案 13 :(得分:0)
如果要在Fragment侧面使用map Fragment,请使用此
SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.flMap, mMapFragment);
fragmentTransaction.commit();
mMapFragment.getMapAsync(this);
答案 14 :(得分:0)
如果您使用的是 Fragment,请更改此代码
SupportMapFragment mapFragment =getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
到
supportMapFragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
同时检查 XML 文件中的更改
android:name="com.google.android.gms.maps.MapFragment"
到
android:name="com.google.android.gms.maps.SupportMapFragment"