我怎样才能制作地图!= null?

时间:2014-05-03 06:18:04

标签: android-fragments android-maps-v2 fragmenttransaction

我在这里写了一个Android片段的代码如下:

Java文件:

package com.WolfDev.ExercisePlanner;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

/**
 * Created by WolfDev on 4/14/2014.
 */
public class TrackerFragment extends Fragment {

private GoogleMap googleMap;
//private boolean chronoRunning = false;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.frag3_action, container, false);

    SupportMapFragment supportMapFragment = SupportMapFragment.newInstance();
    googleMap = supportMapFragment.getMap();
    //googleMap.setMyLocationEnabled(true);
    android.support.v4.app.FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.map, supportMapFragment).commit();
    activityBody();
    return rootView;
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<fragment android:layout_width="match_parent"
          android:layout_height="0px"
          android:layout_weight="6"
          android:id="@+id/map"
          class="com.google.android.gms.maps.MapFragment" >
</fragment>
<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1">
    <Chronometer
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/chronometer"
            android:text="@+id/chronometerDisplay"
            android:gravity="center" />
</LinearLayout>
<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1">
    <Button
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:text="START/STOP"
            android:id="@+id/btnStartStop"
            android:layout_weight="1"/>
    <Button
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:text="LAP/RESET"
            android:id="@+id/btnLapReset"
            android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

正如您在代码中看到的那样,我已将此行注释掉:

//googleMap.setMyLocationEnabled(true);

如果没有注释掉,我会收到NullPointerException错误。我的研究表明,它正在发生,因为变量googleMap为空。我如何更正这一点,使其不再为空?

1 个答案:

答案 0 :(得分:0)

1)你不能拥有

<fragment android:layout_width="match_parent"
      android:layout_height="0px"
      android:layout_weight="6"
      android:id="@+id/map"
      class="com.google.android.gms.maps.MapFragment" >

在您的片段布局文件中。阅读official documentation

==&GT;使用FrameLayout作为地图的容器。

2)您不能指望SupportMapFragment.newInstance()立即返回GoogleMap个实例。交易不会那样工作。

==&GT;在mapFragment.getMap()中致电onResume