错误膨胀SupportMapFragment onConfigurationChanged

时间:2017-01-30 10:38:30

标签: android supportmapfragment onconfigurationchanged

我需要加载正确的布局,具体取决于方向。 但问题是我不想重新加载代码,而只是重新加载布局。

我尝试setContentView(R.layout.activity_main)但是当执行该行时,我收到以下错误:

  • “填充类片段错误”
  • “复制id 0x7f0f00b9,标记为null,或者父ID为0x7f0f00a9,其他片段为com.google.android.gms.maps.SupportMapFragment”

这些错误对应于SupportMapFragment。

我是如何解决的?

此活动有android:configChanges="orientation|keyboardHidden|screenSize"因此,我想在onConfigurationChanged()

中重新加载布局

修改

<RelativeLayout
android:id="@+id/contentLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".activities.MainActivity">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/header">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2.57"
        android:id="@+id/status_layout">

        <TextView
            android:layout_width="271dp"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="NO DISPONIBLE"
            android:id="@+id/status"
            android:gravity="center"
            android:textStyle="bold"
            android:typeface="serif" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.16"
        android:id="@+id/info_layout">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="000-A"
            android:id="@+id/licencia"
            android:layout_weight="1"
            android:gravity="center"
            android:typeface="monospace"
            android:textSize="12sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:text="000-WTF"
            android:id="@+id/matricula"
            android:layout_weight="1"
            android:gravity="center"
            android:typeface="monospace"
            android:textSize="12sp" />
    </LinearLayout>
</LinearLayout>

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.40"
    android:layout_below="@+id/header"
    android:layout_above="@+id/linearLayout" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:id="@+id/linearLayout"
    android:visibility="visible">


    <Button
        android:id="@+id/pickButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white"
        android:text="@string/action_pick_passenger"
        android:layout_alignParentBottom="true"
        android:visibility="visible"
        android:backgroundTint="@color/pick_button"
        android:onClick="onClickPickButton"
        android:textSize="14sp" />

    <Button
        android:id="@+id/destinationButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white"
        android:text="@string/action_on_target"
        android:layout_alignParentBottom="true"
        android:visibility="visible"
        android:backgroundTint="@color/start_button"
        android:onClick="onClickDestinationButton"
        android:textSize="14sp"/>


    <Button
        android:id="@+id/startButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white"
        android:text="@string/action_available"
        android:layout_alignParentBottom="true"
        android:visibility="visible"
        android:backgroundTint="@color/pick_button_pressed"
        android:onClick="onClickStartButton"
        android:textSize="14sp"/>

    <Button
        android:id="@+id/cancelButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white"
        android:text="@string/action_cancel_service"
        android:layout_alignParentBottom="true"
        android:visibility="visible"
        android:backgroundTint="@color/start_button_pressed"
        android:onClick="onClickCancelButton"
        android:textSize="14sp"/>


    <Button
        android:id="@+id/stopButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/action_not_available"
        android:layout_alignParentBottom="true"
        android:visibility="visible"
        android:onClick="onClickStopButton"
        android:textSize="14sp"/>

    <Button
        android:id="@+id/backButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white"
        android:text="@string/action_back_to_service"
        android:layout_alignParentBottom="true"
        android:visibility="visible"
        android:backgroundTint="@color/start_button_pressed"
        android:onClick="onClickBackButton"
        android:textSize="14sp"/>


    <Button
        android:id="@+id/cashButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/cash"
        android:layout_alignParentBottom="true"
        android:visibility="visible"
        android:onClick="cashPayment"
        android:textColor="@android:color/white"
        android:backgroundTint="@color/green"
        android:textSize="14sp"/>


    <Button
        android:id="@+id/paythunderButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/pt_payment"
        android:layout_alignParentBottom="true"
        android:visibility="visible"
        android:onClick="ptPayment"
        android:textColor="@android:color/white"
        android:backgroundTint="@color/pt_rosa"
        android:textSize="14sp"/>


    <Button
        android:id="@+id/tpvButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/tpv_payment"
        android:layout_alignParentBottom="true"
        android:visibility="visible"
        android:onClick="tpvPayment"
        android:textColor="@android:color/white"
        android:backgroundTint="@color/accent"
        android:textSize="14sp"/>


</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_below="@+id/header"
    android:layout_marginTop="50dp"
    android:id="@+id/info"
    android:visibility="gone">

    <ProgressBar
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="20dp"
        android:indeterminate="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text=""
        android:id="@+id/message"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textStyle="bold"
        android:textSize="20dp" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:id="@+id/btn_cancel"
        android:backgroundTint="@android:color/holo_red_dark"
        android:textColor="@color/white"
        android:visibility="gone"
        android:onClick="onClickCancelButton" />
</LinearLayout>

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/snackbarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"/>

<RelativeLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:id="@+id/paypad"
    android:visibility="visible"
    android:background="@color/background_floating_material_dark"
    android:layout_below="@+id/header"
    android:layout_toRightOf="@+id/info"
    android:layout_above="@+id/linearLayout"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_centerHorizontal="true">


    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:columnCount="4"
        android:orientation="horizontal"
        android:id="@+id/number"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true">

        <Button android:text="1"
            android:onClick="btnPressed"
            android:textStyle="bold"
            android:textSize="20dp" />

        <Button android:text="2"
            android:onClick="btnPressed"
            android:textStyle="bold"
            android:textSize="20dp" />

        <Button android:text="3"
            android:onClick="btnPressed"
            android:textStyle="bold"
            android:textSize="20dp" />

        <Button
            android:layout_columnSpan="1"
            android:layout_gravity="fill"
            android:text="0"
            android:onClick="btnPressed"
            android:textStyle="bold"
            android:textSize="20dp" />

        <Button android:text="4"
            android:onClick="btnPressed"
            android:textStyle="bold"
            android:textSize="20dp" />

        <Button android:text="5"
            android:onClick="btnPressed"
            android:textStyle="bold"
            android:textSize="20dp" />

        <Button android:text="6"
            android:onClick="btnPressed"
            android:textStyle="bold"
            android:textSize="20dp" />

        <Button android:text=","
            android:onClick="btnPressed"
            android:textStyle="bold"
            android:textSize="20dp" />

        <Button android:text="7"
            android:onClick="btnPressed"
            android:textStyle="bold"
            android:textSize="20dp" />

        <Button android:text="8"
            android:onClick="btnPressed"
            android:textStyle="bold"
            android:textSize="20dp" />

        <Button android:text="9"
            android:onClick="btnPressed"
            android:textStyle="bold"
            android:textSize="20dp" />

        <Button android:text="DEL"
            android:onClick="btnPressed"
            android:textStyle="bold"
            android:textSize="20dp" />

    </GridLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/layout_cantidad"
        android:layout_above="@+id/number"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="28dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal"
            android:background="@color/material_deep_teal_200"
            android:layout_marginRight="20dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal|right">

                <TextView
                    android:id="@+id/amount"
                    android:layout_height="wrap_content"
                    android:digits="0123456789,."
                    android:ems="15"
                    android:gravity="right"
                    android:hint="@string/amount_000"
                    android:inputType="numberDecimal"
                    android:maxLength="8"
                    android:singleLine="true"
                    android:textSize="50dp"
                    android:layout_width="150dp" />

            </LinearLayout>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.1"
                android:text="@string/simbolo_euro"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="50sp" />
        </LinearLayout>

    </LinearLayout>


</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

使用此

<com.google.android.gms.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

取代:

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.40"
    android:layout_below="@+id/header"
    android:layout_above="@+id/linearLayout" />

在activity.java中,像这样初始化mapview:

   private MapView mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(this);
    }

     @Override
        public void onResume() {
            mapView.onResume();
            super.onResume();
        }

        @Override
        public void onPause() {
            super.onPause();
            mapView.onPause();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            mapView.onDestroy();
        }

        @Override
        public void onLowMemory() {
            super.onLowMemory();
            mapView.onLowMemory();
        }