我尝试了所有的东西,但它仍然崩溃了。有人请帮助我。这是我的地图代码。 我成功实现了具有片段活动和支持片段的地图(作为单独的项目)。但是当我试图在我们的主项目中实现这是一个片段而不是片段活动时,我尝试使用片段管理器等但都是徒劳的:(
我做了我在其他帖子中看到的所有内容,例如使用mapview,尝试使用catch for layout inflate ..但没有任何效果
我的完整代码非常冗长。我想在该代码中实现一个地图视图/支持片段。所以我只写了实现map的部分。并在哪里发生问题。我这在布局上有一些问题。因为每次都会发生布局崩溃。
public class EventFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.workspace_event_fragment_layout, container, false);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
mapView = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (mapView == null) {
mapView = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, mapView).commit();
}
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (theMap == null) {
theMap = mapView.getMap();
theMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
}
}
in xml
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.google.android.gms.maps.SupportMapFragment" />
此片段位于滚动布局 - >线性布局之间 我收到了错误:
07-22 16:37:33.845: W/System.err(21217): android.view.InflateException: Binary XML file line #176: Error inflating class fragment
07-22 16:37:33.845: W/System.err(21217): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
07-22 16:37:33.845: W/System.err(21217): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
07-22 16:37:33.845: W/System.err(21217): at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
07-22 16:37:33.845: W/System.err(21217): at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
07-22 16:37:33.845: W/System.err(21217): at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
07-22 16:37:33.845: W/System.err(21217): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
07-22 16:37:33.845: W/System.err(21217): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
07-22 16:37:33.845: W/System.err(21217): at com.cordiant.cordiantlearn.workspace.WorkspaceEventFragment.onCreateView(WorkspaceEventFragment.java:204)
07-22 16:37:33.845: W/System.err(21217): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-22 16:37:33.845: W/System.err(21217): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
07-22 16:37:33.845: W/System.err(21217): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
07-22 16:37:33.845: W/System.err(21217): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-22 16:37:33.845: W/System.err(21217): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
07-22 16:37:33.845: W/System.err(21217): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
07-22 16:37:33.845: W/System.err(21217): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
07-22 16:37:33.845: W/System.err(21217): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
07-22 16:37:33.845: W/System.err(21217): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
07-22 16:37:33.845: W/System.err(21217): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
07-22 16:37:33.845: W/System.err(21217): at android.view.View.measure(View.java:16831)
07-22 16:37:33.845: W/System.err(21217): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
07-22 16:37:33.845: W/System.err(21217): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-22 16:37:33.845: W/System.err(21217): at android.view.View.measure(View.java:16831)
07-22 16:37:33.845: W/System.err(21217): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:651)
07-22 16:37:33.845: W/System.err(21217): at android.view.View.measure(View.java:16831)
07-22 16:37:33.845: W/System.err(21217): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
07-22 16:37:33.845: W/System.err(21217): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-22 16:37:33.845: W/System.err(21217): at android.view.View.measure(View.java:16831)
07-22 16:37:33.845: W/System.err(21217): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
07-22 16:37:33.845: W/System.err(21217): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
07-22 16:37:33.845: W/System.err(21217): at android.view.View.measure(View.java:16831)
07-22 16:37:33.845: W/System.err(21217): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
07-22 16:37:33.845: W/System.err(21217): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-22 16:37:33.845: W/System.err(21217): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
07-22 16:37:33.845: W/System.err(21217): at android.view.View.measure(View.java:16831)
07-22 16:37:33.845: W/System.err(21217): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2189)
07-22 16:37:33.850: W/System.err(21217): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1352)
07-22 16:37:33.850: W/System.err(21217): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1535)
07-22 16:37:33.850: W/System.err(21217): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
07-22 16:37:33.850: W/System.err(21217): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
07-22 16:37:33.850: W/System.err(21217): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
07-22 16:37:33.850: W/System.err(21217): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
07-22 16:37:33.850: W/System.err(21217): at android.view.Choreographer.doFrame(Choreographer.java:561)
07-22 16:37:33.850: W/System.err(21217): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
07-22 16:37:33.850: W/System.err(21217): at android.os.Handler.handleCallback(Handler.java:730)
07-22 16:37:33.850: W/System.err(21217): at android.os.Handler.dispatchMessage(Handler.java:92)
07-22 16:37:33.850: W/System.err(21217): at android.os.Looper.loop(Looper.java:176)
07-22 16:37:33.850: W/System.err(21217): at android.app.ActivityThread.main(ActivityThread.java:5419)
07-22 16:37:33.850: W/System.err(21217): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 16:37:33.850: W/System.err(21217): at java.lang.reflect.Method.invoke(Method.java:525)
07-22 16:37:33.850: W/System.err(21217): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
07-22 16:37:33.850: W/System.err(21217): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
07-22 16:37:33.850: W/System.err(21217): at dalvik.system.NativeStart.main(Native Method)
07-22 16:37:33.850: W/System.err(21217): Caused by: java.lang.IllegalStateException: Fragment com.google.android.gms.maps.SupportMapFragment did not create a view.
07-22 16:37:33.850: W/System.err(21217): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:314)
07-22 16:37:33.850: W/System.err(21217): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
07-22 16:37:33.850: W/System.err(21217): ... 51 more
我的完整布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/greyBG"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/greyBG"
android:orientation="vertical"
android:padding="8dp" >
<LinearLayout
android:id="@+id/workspace_eventTab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/workspace_profile_image"
android:layout_width="65dp"
android:layout_height="65dp"
android:src="@drawable/ic_prof_loading" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:orientation="vertical" >
<TextView
android:id="@+id/workspace_event_subject_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/workspace_event_subject_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="Subject" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/workspace_event_expand_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible" >
<TextView
android:id="@+id/workspace_desc_event_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/workspace_desc_event_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="Description" />
<CheckBox
android:id="@+id/allDay_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="All Day" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/workspace_event_startDate_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Date"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/workspace_event_startDatePicker_btn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@color/transparent"
android:textColor="@color/calenderGreen"
android:textSize="21sp" />
<Button
android:id="@+id/workspace_event_startTimePicker_btn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@color/transparent"
android:textColor="@color/actionbar_bg_color"
android:textSize="21sp" />
</LinearLayout>
<EditText
android:id="@+id/workspace_event_startDate_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="Start Date" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/workspace_event_endDate_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Date"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginRight="50dp" />
<Button
android:id="@+id/workspace_event_endDatePicker_btn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@color/transparent"
android:textColor="@color/calenderGreen"
android:textSize="21sp" />
<Button
android:id="@+id/workspace_event_endTimePicker_btn"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@color/transparent"
android:textColor="@color/actionbar_bg_color"
android:textSize="21sp" />
</LinearLayout>
<EditText
android:id="@+id/workspace_event_endDate_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="End Date" />
<TextView
android:id="@+id/workspace_event_location_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginRight="50dp" />
<com.cordiant.cordiantlearn.workspace.CustomAutoCompleteTextView
android:id="@+id/workspace_event_location_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="Location"
android:inputType="textCapSentences"/>
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<EditText
android:id="@+id/workspace_event_room_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Room"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/workspace_event_room_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="Room" />
<View
android:id="@+id/view_for_mapview"
android:layout_width="match_parent"
android:layout_height="1dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Invitees"
android:textSize="20sp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/lightGray" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="vertical" >
<RadioButton
android:id="@+id/radioBtn_pg_event_layout"
android:layout_marginTop="15dp"
android:text="Performance Group"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:text="Invite one or more Performance Groups to this Event"
android:textColor="@color/gray" />
<AutoCompleteTextView
android:id="@+id/atv_pg_event_workspace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
<ListView
android:id="@+id/lv_pg_event_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" >
</ListView>
<RadioButton
android:id="@+id/radioBtn_people_event_layout"
android:layout_marginTop="15dp"
android:text="Specific People"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:text="Invite one or more People to this Event"
android:textColor="@color/gray" />
<AutoCompleteTextView
android:id="@+id/atv_people_event_workspace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
<ListView
android:id="@+id/lv_people_event_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" >
</ListView>
</RadioGroup>
<CheckBox
android:id="@+id/restrictedAccessCheckbox_event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restricted Access"
android:textSize="18dp"
android:textStyle="bold" />
<CheckedTextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="* If it is marked as 'Restricted Access', then it will be accessible only within the Performance Groups or Specific People to which it has been shared."/>
<CheckedTextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="* The Content with restricted access will not come up in the Search results if the User is not part of that Content."/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/workspace_createBtn_event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="@drawable/browse_button_drawable"
android:text="Create Event"
android:textColor="@color/white"
android:textStyle="bold" />
<Button
android:id="@+id/workspace_closeBtn_event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/browse_button_drawable"
android:text="Close"
android:textColor="@color/white"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
答案 0 :(得分:4)
解决:
使用<FrameLayout/>
代替<Fragment/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.03"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/mapwhere" />
和
public class WhereFragment extends SupportMapFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.where, null, false);
initilizeMap();
return root;
}
private void initilizeMap()
{
mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapwhere);
if (mSupportMapFragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mSupportMapFragment = SupportMapFragment.newInstance();
fragmentTransaction.replace(R.id.mapwhere, mSupportMapFragment).commit();
}
if (mSupportMapFragment != null)
{
googleMap = mSupportMapFragment.getMap();
if (googleMap != null)
googleMap.setOnMapClickListener(new OnMapClickListener()
{
@Override
public void onMapClick(LatLng point)
{
//TODO: your onclick stuffs
}
});
}
}
}
答案 1 :(得分:2)
不支持您尝试做的事情。见here。这来自开发人员文档:
注意:当布局包含a时,您无法将布局扩展为片段。只有动态添加到片段时才支持嵌套片段。
它基本上说的如下。考虑一个 MyClass
类public class MyClass extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_myclass, container,
false);
return view;
}
}
布局文件 fragment_myclass 不能包含&lt; fragment /&gt; 标记为元素。