2014-05-03 15 views
1

我在这里写了一个Android碎片下面的代码:我该如何去做map!= null?

的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为空。我该如何纠正这一点,使其不再为空?

回答

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

==>使用FrameLayout作为地图的容器。

2)你不能指望SupportMapFragment.newInstance()立即返回GoogleMap实例。事务不会那样工作。

==>通话中onResumemapFragment.getMap()

+0

你在哪里得到来自mapFragment?我没有这个名字的变量。 – Patr3xion

+0

@ Patr3xion我想你会想将'SupportMapFragment.newInstance()'分配给一个字段而不是局部变量。 –