我第一次打开地图片段时,单击操作栏图标,搜索位置,在该位置添加标记,并在单击标记时将位置名称添加到列表视图。但是,当我再次点击操作栏图标搜索另一个地方我的应用程序崩溃。请帮助,我已经解决了其他类似的问题,但没有任何效果。提前谢谢。
主要活动
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends ActionBarActivity implements GoogleMap.OnMarkerClickListener{
GoogleMap mMap;
private static final float DEFAULT_ZOOM = 14;
public static ArrayList<String> myPlaces = new ArrayList<String>();
public static String location;
public ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_addPlaces:
setContentView(R.layout.map_fragment);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
default:
return super.onOptionsItemSelected(item);
}
}
public boolean initMap(){
if(mMap==null){
SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFrag.getMap();
}
return(mMap!=null);
}
private void gotoLocation(double lat, double lng, float zoom){
LatLng ll = new LatLng(lat,lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
mMap.moveCamera(update);
}
public void geoLocate(View v) throws IOException {
// hideSoftKeyboard(v);
EditText et = (EditText) findViewById(R.id.editText1);
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
location = et.getText().toString();
if (location.length() == 0) {
Toast.makeText(this, "Please enter a valid location", Toast.LENGTH_LONG).show();
return;
}
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(location, 1);
Address add = list.get(0);
String locality = add.getLocality();
Toast.makeText(this, locality, Toast.LENGTH_LONG).show();
double lat = add.getLatitude();
double lng = add.getLongitude();
gotoLocation(lat, lng, DEFAULT_ZOOM);
MarkerOptions options = new MarkerOptions()
.title(location)
.position(new LatLng(lat, lng));
mMap.addMarker(options);
mMap.setOnMarkerClickListener(this);
}
public boolean onMarkerClick (Marker marker){
PlaceholderFragment list1 = new PlaceholderFragment();
myPlaces.add(location);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.add(R.id.container,list1)
.commit();
return false;
}
public static class PlaceholderFragment extends Fragment {
ArrayAdapter<String> mPlacesAdapter;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ArrayAdapter mPlacesAdapter = new ArrayAdapter<String>(
getActivity(),
R.layout.list_item_places,
R.id.list_item_places_textview,
MainActivity.myPlaces);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.listView_Places);
listView.setAdapter(mPlacesAdapter);
return rootView;
}
}
}
地图片段布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="@dimen/abc_action_bar_subtitle_bottom_margin_material"
android:paddingBottom="@dimen/abc_action_bar_subtitle_bottom_margin_material">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_gravity="left|center_vertical"
android:text="Location:"
android:textSize="20sp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:layout_gravity="left|bottom"
android:ems="10">
<requestFocus/>
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:id="@+id/button1"
android:text="GO"
android:onClick="geoLocate"/>
</LinearLayout>
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:value="@integer/google_play_services_version"
map:cameraTargetLat="17.422245"
map:cameraTargetLng="78.382000"
map:cameraZoom="10"/>
ListView布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView_Places"
android:layout_gravity="left|center_horizontal" />
</FrameLayout>