在片段中获取Map View时导航到另一个片段并返回到地图视图时出现错误。我创建了一个替换片段的对象但是我已经添加了log cat片段
package com.Raja.Avantaj;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import android.app.Fragment;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MapFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.mapfragment, container, false);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
MapView map = new MapView(getActivity(), "0LYqpa9x_HkkUzATV8Rt5MHwhUuvm7sbnxsvZWQ");
map.setClickable(true);
map.getController().setCenter(new GeoPoint((int)(8.532138 * 1E6),(int)(76.961583 * 1E6)));
map.getController().setZoom(9);
map.setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(R.drawable.balloon);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
map.getOverlays().add(new AvantajOverLay(marker));
MyLocationOverlay me=new MyLocationOverlay(getActivity(), map);
map.getOverlays().add(me);
((ViewGroup)getView()).addView(map);
}
}
我在这里调用片段
package com.Raja.Avantaj;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
/**
* @author user5
*
*/
public class ContactUs extends Fragment {
Button mapus;
View v;
Fragment frag1;
/* (non-Javadoc)
* @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
v = inflater.inflate(R.layout.contactus, container, false);
intilize();
return v;
}
// add a button to display the map
private void intilize() {
// TODO Auto-generated method stub
mapus = (Button)v.findViewById(R.id.map);
mapus.setOnClickListener(btnOnClickListener);
}
Button.OnClickListener btnOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*Toast.makeText(getActivity(),"Why didn't come", Toast.LENGTH_LONG).show();
Intent intent=new Intent(v.getContext(),MapUs.class);
ContactUs.this.startActivity(intent);*/
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
if(getFragmentManager().findFragmentByTag("MapFragment")==null)
{
Log.d("in null","in null" );
frag1 = new MapFragment();
fragmentTransaction.replace(R.id.layoutfordetails, frag1,"MapFragment");
//fragmentTransaction.addToBackStack("MapFragment");
}
else
{
Log.d("in not null","in not null" );
Fragment f11 = getFragmentManager().findFragmentByTag("MapFragment");
fragmentTransaction.replace(R.id.layoutfordetails, f11,"MapFragment");
//fragmentTransaction.addToBackStack("MapFragment");
}
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
}
};
}
我得到这个Logcat错误
03-19 13:16:45.214: W/dalvikvm(425): threadid=1: thread exiting with uncaught exception (group=0x40014760)
03-19 13:16:45.294: E/AndroidRuntime(425): FATAL EXCEPTION: main
03-19 13:16:45.294: E/AndroidRuntime(425): java.lang.IllegalStateException: Fragment already added: MapFragment{41000090 #2 id=0x7f080018 MapFragment}
03-19 13:16:45.294: E/AndroidRuntime(425): at android.app.BackStackRecord.doAddOp(BackStackRecord.java:322)
03-19 13:16:45.294: E/AndroidRuntime(425): at android.app.BackStackRecord.replace(BackStackRecord.java:360)
03-19 13:16:45.294: E/AndroidRuntime(425): at com.Raja.Avantaj.ContactUs$1.onClick(ContactUs.java:70)
03-19 13:16:45.294: E/AndroidRuntime(425): at android.view.View.performClick(View.java:3110)
答案 0 :(得分:0)
创建MapFragment的新实例,即使它不为null。