我正在尝试从活动中加载地图片段。
活动代码:
package com.example.project;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class VehicleListActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list_viewer);
ListView list = getListView();
ListAdapter adapter = new ListAdapter(getApplicationContext());
list.setAdapter(adapter);
list.setItemsCanFocus(true);
list.setOnItemClickListener
(
new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "you hav clicked", Toast.LENGTH_LONG).show();
Adapter adapter = arg0.getAdapter();
String vehicleNumber = ((VehicleDetails) adapter.getItem(arg2)).getVehicleNumber();
double latitude = ((VehicleDetails) adapter.getItem(arg2)).getLatitude();
double longitude = ((VehicleDetails) adapter.getItem(arg2)).getLongitude();
Bundle data = new Bundle();
data.putString("VEHICLE_No", vehicleNumber);
data.putDouble("LATITUDE", latitude);
data.putDouble("LONGITUDE", longitude);
MapLoaderFragment map = new MapLoaderFragment();
map.setArguments(data);
}
}
);
}
}
片段类代码:
package com.example.project;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MapLoaderFragment extends Fragment {
MapView mapView;
GoogleMap map;
public MapLoaderFragment()
{
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
String vehicleNumber = getArguments().getString("VEHICLE_NO");
double latitude = getArguments().getDouble("LATITUDE");
double longitude = getArguments().getDouble("LONGITUDE");
View view = inflater.inflate(R.layout.map_fragment, container, false);
mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
map = mapView.getMap();
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
LatLng position = new LatLng(latitude, longitude);
Marker marker = map.addMarker(new MarkerOptions());
marker.setTitle(vehicleNumber);
marker.setPosition(position);
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.car));
CameraPosition cameraPosition = new CameraPosition.Builder().target(
position).zoom(15).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
return view;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
mapView.onDestroy();
super.onDestroy();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
mapView.onResume();
super.onResume();
}
}
这是我到目前为止所做的。在主要活动中我加载列表,添加点击监听器列表。从点击我试图通过发送纬度经度和车辆号码加载地图。
MainActivity.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
地图片段代码:
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
答案 0 :(得分:2)
主要有两种方法可以将片段用于活动。
静态:在活动的布局文件中声明片段。
或者
动态:以编程方式将片段添加到现有。
在您的代码中似乎您没有以编程方式插入片段,那么您的问题将在您的xml布局中。
此处的基本示例:http://developer.android.com/guide/components/fragments.html
答案 1 :(得分:1)
您没有将MapLoaderFragment
放置到任何片段容器中。通常,您的XML布局文件中将有一个容器来容纳该片段,然后在您将拥有的调用活动中
MapLoaderFragment map = new MapLoaderFragment();
map.setArguments(data);
...
FragmentManager fm = getFragmentManager();
fm.beginTransaction()
.add(container, map)
.commit();