Android fragments 我怎样才能开始制作地图!=无效的
我在这里为android片段编写了以下代码: Java文件:Android fragments 我怎样才能开始制作地图!=无效的,android-fragments,android-maps-v2,fragmenttransaction,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.andr
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>
当它没有被注释掉时,我得到一个NullPointerException错误。我的研究表明,这是因为变量googleMap为空。如何更正此项,使其不再为空?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" >
在片段的布局文件中。阅读
==>使用FrameLayout
作为地图的容器
2) 您不能期望SupportMapFragment.newInstance()
立即返回GoogleMap
实例。交易不是这样的
==>在
onResume
中调用mapFragment.getMap()
?我没有同名的变量。@Patr3xion我想您应该将SupportMapFragment.newInstance()
赋值给一个字段,而不是局部变量。
<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" >