【发布时间】:2015-11-03 14:37:02
【问题描述】:
我知道周围有很多这样的帖子,但没有任何解决方案可以帮助我。
我有一个带有两个片段的浏览器,第二个是我的地图。 但是每次我尝试在该地图上绘制一条折线时,我都会遇到一个空点异常。 只有当我使用 setContentView 时,它才能找到我的地图片段(但这会破坏我的 viewpager)。
我的部分活动:
public void buttonMaps_Click(View view) {
// setContentView(R.layout.fifth_fragment);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
if (mapFragment != null) {
GoogleMap map = mapFragment.getMap();
// map1 = ((SupportMapFragment)
// getSupportFragmentManager().findFragmentById(R.id.map1)).getMap();
Polyline line = map.addPolyline(
new PolylineOptions().add(mP0, mP1, mP2, mP3, mP4, mP5, mP6, mP7, mP8).width(5).color(Color.BLUE));
}
}
我的片段 xml。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fifth_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="312dp"
android:tag="lumpeneimer" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/map"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:onClick="buttonMaps_Click"
android:text="Button" />
</RelativeLayout>
有没有不使用 setContentView 来绘制折线的方法?
编辑:我在我的 fragment.java 上试过这个:
public class FifthFragment extends Fragment implements OnMapReadyCallback {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstaceState) {
// TODO Auto-generated method stub
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return (LinearLayout) inflater.inflate(R.layout.fifth_fragment, container, false);
@Override
public void onMapReady(GoogleMap googleMap) {
GoogleMap map = googleMap;
// TODO Auto-generated method stub
Polyline line = map.addPolyline(
new PolylineOptions().add(mP0, mP1, mP2, mP3, mP4, mP5, mP6, mP7, mP8).width(5).color(Color.BLUE));
}
但仍然在以下位置获得 NullPoint:
mapFragment.getMapAsync(this);
【问题讨论】:
-
NullPointerException的解决方案总是一样的,找到为 null 的值并初始化它,或者不要尝试访问它的变量或方法。 -
您收到此错误是因为未初始化折线。
-
错误是因为“mapFragment”为空。它没有得到地图片段
-
@KevinEsche 我知道哪个值,如果我使用 setContentView 它可以工作,但这会破坏我的 Viewpager。
标签: java android google-maps fragment