我正在尝试将地图视图附加到选项卡中的片段。
这是我的 MapFragment:
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import com.actionbarsherlock.app.SherlockFragment;
public class MapFragment extends SherlockFragment {
private static final String KEY_STATE_BUNDLE = "localActivityManagerState";
private LocalActivityManager mLocalActivityManager;
protected LocalActivityManager getLocalActivityManager() {
return mLocalActivityManager;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle state = null;
if (savedInstanceState != null) {
state = savedInstanceState.getBundle(KEY_STATE_BUNDLE);
}
mLocalActivityManager = new LocalActivityManager(getActivity(), true);
mLocalActivityManager.dispatchCreate(state);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//This is where you specify you activity class
Intent i = new Intent(getActivity(), LocationActivity.class);
Window w = mLocalActivityManager.startActivity("tag", i);
View currentView=w.getDecorView();
currentView.setVisibility(View.VISIBLE);
currentView.setFocusableInTouchMode(true);
((ViewGroup) currentView).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
return currentView;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBundle(KEY_STATE_BUNDLE,
mLocalActivityManager.saveInstanceState());
}
@Override
public void onResume() {
super.onResume();
mLocalActivityManager.dispatchResume();
}
@Override
public void onPause() {
super.onPause();
mLocalActivityManager.dispatchPause(getActivity().isFinishing());
}
@Override
public void onStop() {
super.onStop();
mLocalActivityManager.dispatchStop();
}
@Override
public void onDestroy() {
super.onDestroy();
mLocalActivityManager.dispatchDestroy(getActivity().isFinishing());
}
}
这是我的位置活动:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
/*
* A location activity that defers location and map functionality to the app. This activity can be used directly but an activity have to send lon/lat when they do the intent call.
* (non-Javadoc)
* @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
*/
public class LocationActivity extends com.actionbarsherlock.app.SherlockMapActivity {
/*t
* Variables for this activity.
*/
private MapView mapView;
private GeoPoint newP, mapOverlayP, myLocationP,p,markedP;
private boolean pointset = false, markset = false, point = false, markerpoint = false;
private ArrayList<String> address = new ArrayList<String>();
private ArrayList<Double> lon = new ArrayList<Double>();
private ArrayList<Double> lat = new ArrayList<Double>();
private String markedAddr = null,setAddr = null;
private MapController mc;
private double latitude, longitude, Lon, Lat;
private ArrayList<LocationDb> locations = new ArrayList<LocationDb>();
LocationDb locationDb = new LocationDb();
private Bundle bundle = new Bundle();
LocationManager locationManager;
Location location,lastLocation,myLocation;
long minTime = 3;
float bestAccuracy =1;
Location bestResult;
long bestTime = 0;
private LocationListener locationListener;
public ArrayList<Place> places = new ArrayList<Place>();
ListView placesListView;
Button expandListButton;
public static EditText searchText;
@Override
public void onStart(){
final View customNav = LayoutInflater.from(this).inflate(R.layout.actionbar_title, null);
final TextView customNavHeader = (TextView) customNav.findViewById(R.id.actionbarHeader);
customNavHeader.setText("Done");
customNavHeader.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
onClick_addLocation();
}
}); //e
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setCustomView(customNav);
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mapView =(MapView)findViewById(R.id.mapView1);
expandListButton = (Button)findViewById(R.id.expandListButton);
new MyLocationOverlay(this, mapView);
placesListView = (ListView)findViewById(R.id.explorePlacesListView);
// Define a listener that responds to location updates
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location
// provider.
// makeUseOfNewLocation(location);
getLonLat(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
getLocation();
//handler = new Handler();
// bundle = (Bundle) getIntent().getExtras().getBundle("bundle");
bundle = null;
if (bundle != null)
{
getLocation();
}
checkLocation();
if(myLocationP != null)
{
addMyLocationMarker(myLocationP);
}
if(markset)
{
addLocationMarker(newP);
}
new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
//Menu
placesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
latitude = Double.valueOf(places.get(position).latitude);
longitude = Double.valueOf(places.get(position).longitude);
newP = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
markedP = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
markedAddr = places.get(position).name;
setAddr = places.get(position).name;
Toast.makeText(getApplicationContext(), markedAddr, Toast.LENGTH_LONG).show();
addLocationMarker(newP);
pointset = false;
}
});
expandListButton.setText("Searching..");
}
只是无法将其附加到片段。我哪里错了?
另外,这是最好的方法吗?我不能将它附加到片段本身而不是调用另一个活动吗?如果是这样,怎么做?