我有一个应用程序,在里面我必须显示一个片段(来自v4 suppport库),里面有一个地图和一个位置。我使用了play服务库,并创建了包含支持映射片段的XML和
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
我在我的应用程序中使用多个片段和FrameLayout
,我在其中进行片段事务。
我已经定义了这样的位置片段:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class LocationFragment extends Fragment{
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
RelativeLayout rl;
private GoogleMap map=null;
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceBundle)
{
if (map==null){
rl = (RelativeLayout) inflater.inflate(R.layout.maps, container,false);
map = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Pilates")
.snippet("Pilates teaching")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
return rl;
}
}
当我第一次点击激活位置片段的按钮时,一切正常。当我点击另一个片段然后再次在此片段上出现问题时,我收到以下错误:
E/AndroidRuntime(30571): FATAL EXCEPTION: main
E/AndroidRuntime(30571): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
E/AndroidRuntime(30571): at android.view.ViewGroup.addViewInner(ViewGroup.java:3620)
E/AndroidRuntime(30571): at android.view.ViewGroup.addView(ViewGroup.java:3491)
E/AndroidRuntime(30571): at android.view.ViewGroup.addView(ViewGroup.java:3436)
E/AndroidRuntime(30571): at android.view.ViewGroup.addView(ViewGroup.java:3412)
E/AndroidRuntime(30571): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
E/AndroidRuntime(30571): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
E/AndroidRuntime(30571): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
E/AndroidRuntime(30571): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
E/AndroidRuntime(30571): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
E/AndroidRuntime(30571): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
E/AndroidRuntime(30571): at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(30571): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(30571): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(30571): at android.app.ActivityThread.main(ActivityThread.java:4921)
E/AndroidRuntime(30571): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(30571): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(30571): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
E/AndroidRuntime(30571): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
E/AndroidRuntime(30571): at dalvik.system.NativeStart.main(Native Method)
E/android.os.Debug( 2143): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
你能帮我解决这个问题吗?
答案 0 :(得分:0)
试试这个:
rl = (RelativeLayout) inflater.inflate(R.layout.maps, null);