我已经创建了导航抽屉,并且每个列表项都会加载一个片段。有一个家庭片段,将加载地图。我在活动中加载了地图,扩展了片段活动但不在片段内部。我收到错误但没有得到它所以需要建议。
// HomeFragment.java
public class HomeFragment extends Fragment implements OnInfoWindowClickListener {
GoogleMap myMap;
private Marker marker;
private Marker marker2;
private Marker marker3;
// GPSTracker class
GpsTracker gps;
// textview
TextView DestinationDistance;
double latitude;
double longitude;
double lat2 = 23.7818442;
double lng2 = 90.3245161;
double lat3 = 23.7818442;
double lng3 = 90.4245161;
private Polyline line;
private ViewGroup userLayout;
// user info views
private TextView txtDistance;
private TextView txtETA;
private int count = 0;
private ArrayList<Polyline>pl;
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
userLayout = (ViewGroup) v.findViewById(R.id.userLayout);
txtDistance = (TextView) v.findViewById(R.id.txtV2);
txtETA = (TextView) v.findViewById(R.id.txtV3);
myMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
myMap.setOnInfoWindowClickListener(this);
/*Here will be other code to execute asynctask,
location detect, add marker etc */
return v;
}
@Override
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
}
}
*对于此代码我遇到错误
'The method getSupportFragmentManager() is undefined for the type HomeFragment'
// fragment_home.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="@+id/userLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.0"
android:orientation="horizontal"
android:visibility="gone" >
<LinearLayout
android:id="@+id/user_infoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="#fff" >
<TextView
android:id="@+id/txtV1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="#000"
android:text="Profile Picture" />
</LinearLayout>
<LinearLayout
android:id="@+id/position_infoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >
<TextView
android:id="@+id/txtV2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:text="" />
<TextView
android:id="@+id/txtV3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:text="" />
<Button
android:id="@+id/btnMsg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:text="Send Message" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:1)
你试过这个: -
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp" />
并使用:
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
获取片段
答案 1 :(得分:0)
尝试下面的代码,我已经用这种方式做了它的工作正常。
lnrMap.setVisibility(View.VISIBLE);
mapFragment = new SupportMapFragment();
addFragment(lnrMap.getId(), mapFragment);
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
googleMap = mapFragment.getExtendedMap();
mapFragment.getView().setClickable(false);
if (googleMap != null) {
googleMap.getUiSettings().setZoomControlsEnabled(false);
googleMap.getUiSettings().setCompassEnabled(false);
googleMap.getUiSettings().setZoomGesturesEnabled(false);
googleMap.getUiSettings().setScrollGesturesEnabled(false);
googleMap.getUiSettings().setRotateGesturesEnabled(false);
googleMap.getUiSettings().setTiltGesturesEnabled(false);
googleMap.animateCamera(CameraUpdateFactory.zoomTo(6));
t.cancel();
}
} catch (Exception e) {
}
}
});
}
}, 0, 500);
//Just declare one LinearLayout in your xml file with the name of lnrMap, and then above //code will add map in that Linearlayout programatically.
//Here is sample xml block for lnrMap LinearLayout.
<LinearLayout
android:id="@+id/lnrMap"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical"
android:visibility="gone" >
//addFragment method:
public void addFragment(int layoutId, Fragment fragment) {
FragmentManager fm = getChildFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(layoutId, fragment);
ft.commit();
}
答案 2 :(得分:0)
转到此处,您将找到解决方案
The method getSupportFragmentManager() is undefined for the type MainActivity for Actionbarsherlock
答案 3 :(得分:0)
首先检查您的Android最低SDK版本。如果是below < 11
,则使用SupportMapFragment
,如果是above >11
,则使用MapFragment
。
其次,请检查是否已将片段导入为android.support.app.v4