【问题标题】:XML Error when using Fragment tag in android在android中使用Fragment标签时出现XML错误
【发布时间】:2015-05-19 09:32:47
【问题描述】:

我正在尝试在我的代码中实现片段标记,以便我可以将它与 GoogleMap API 一起使用。然而,一开始我就因为“二进制 XML 文件行”错误而陷入困境。 我尝试在论坛中搜索它,但我无法解决它。 请帮助我理解这个问题。

我将所有文件附加到我的 activity_main.xml

<LinearLayout 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="com.example.mapdemo.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>

我的 activity_map.xml 文件是这样的

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView 
            android:text="Loc:"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            />

        <EditText 
            android:id="@+id/edt_locationName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|center_vertical"
            android:ems="10"/>
        <Button 
            android:id="@+id/btn_GetLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Go"
            android:layout_gravity="right|center_vertical"
            android:onClick="geoLocate"/>

    </LinearLayout>

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </fragment>

</LinearLayout>

我的主要 java 文件是这样的

    package com.example.mapdemo;

import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends FragmentActivity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

日志猫如下:-

05-19 14:45:15.763: E/AndroidRuntime(28629): FATAL EXCEPTION: main
05-19 14:45:15.763: E/AndroidRuntime(28629): Process: com.example.mapdemo, PID: 28629
05-19 14:45:15.763: E/AndroidRuntime(28629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapdemo/com.example.mapdemo.MainActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class fragment
05-19 14:45:15.763: E/AndroidRuntime(28629):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
05-19 14:45:15.763: E/AndroidRuntime(28629):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2498)
05-19 14:45:15.763: E/AndroidRuntime(28629):    at android.app.ActivityThread.access$900(ActivityThread.java:179)
05-19 14:45:15.763: E/AndroidRuntime(28629):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)

【问题讨论】:

  • 您是否设置了使用地图所需的所有东西??
  • 不,我试图一步一步地做,因为这没有运行我没有在清单文件中设置任何其他东西
  • 我已经编辑了我的答案。请试一试。它可能会起作用。
  • 发布完整的堆栈跟踪,包括嵌套的“由”异常。
  • 你需要设置它才能让它工作,因为它连接到谷歌 gms 你还需要为你的清单添加一些权限 结帐这个developers.google.com/maps/documentation/android/start 也结帐这个链接stackoverflow.com/questions/14825331/…

标签: java android xml android-fragments


【解决方案1】:

在您的 MainActivity 中,添加以下导入语句

import android.support.v4.app.Fragment;

编辑:我不确定这是否可行,但请尝试将您的片段标签更改为此

<fragment
    android:id="@+id/map"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</fragment>

【讨论】:

    【解决方案2】:

    尝试将片段放在linearLayout标签内

    【讨论】:

      猜你喜欢
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 2013-12-16
      • 1970-01-01
      相关资源
      最近更新 更多