首页 > 解决方案 > 无法解释或解决 XML 中的片段元素的问题

问题描述

到目前为止,该应用程序只有一种布局,activity_main.xml.

该应用程序在连接 USB 的开发人员 Android 手机上运行但几乎立即退出。

我尝试对应用程序进行调试和故障排除,但结果始终相同:应用程序不断退出。

最后,我尝试编辑应用程序中唯一的布局,activity_main.xml. 我开始添加、删除和交换元素。

我发现fragment删除元素后应用程序运行良好,我不知道该问题的解释或解决方案。

这是activity_main.xml下面的。fragment我提到 的元素具有supportMap id属性。

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<RelativeLayout 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"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/driverStatusLayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/totalOnlineDrivers"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="15dp"
            android:gravity="center"
            android:text="@string/total_online_drivers"
            android:textColor="@color/common_google_signin_btn_text_dark"
            android:textSize="22sp" />

        <android.support.v7.widget.SwitchCompat
            android:id="@+id/driverStatusSwitch"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:layout_marginRight="15dp"
            android:checked="false" />
    </FrameLayout>

    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/supportMap"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/driverStatusLayout"
        tools:context="spartons.com.frisbeeGo.fragments.MapFragment" />

</RelativeLayout>
</android.support.constraint.ConstraintLayout>

这是MainActivity.java

package com.example.testbasna;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MainActivity extends AppCompatActivity {

    private GoogleMap googleMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}

标签: androidxmlandroid-fragments

解决方案


推荐阅读