如何正确膨胀片段?

时间:2017-12-29 04:31:32

标签: android android-fragments

This code会在其上显示Google地图及其中的一些标记。它在我的Galaxy Note 4上运行得很好。奇怪的是,它与我的应用程序集成时会崩溃。

MapActivity.java

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

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
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.MarkerOptions;

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {

    SupportMapFragment mapFragment;

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

        //  mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.trisakti_map);
        //  mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(-6.166434, 106.79781))
                .title("Universitas Trisakti"));

    }
}

activity_map.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="app.devbyzero.net.pascasarjanatrisakti.MapActivity"
   >

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


</RelativeLayout>
logcat说:

  

12-29 11:00:26.893 3623-3623 /? E / AndroidRuntime:致命异常:主要                                                    流程:app.devbyzero.net.pascasarjanatrisakti,PID:3623                                                    java.lang.RuntimeException:无法启动活动   ComponentInfo {app.devbyzero.net.pascasarjanatrisakti / app.devbyzero.net.pascasarjanatrisakti.MapActivity}:   android.view.InflateException:二进制XML文件行#0:二进制XML   文件行#0:错误膨胀类片段                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)                                                        在android.app.ActivityThread.access $ 1100(ActivityThread.java:221)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1794)

当然,当注释xml中的片段部分时,没有崩溃。很奇怪,因为我从一个工作示例中获取了这个片段代码:/

那么,我该如何解决这个膨胀问题?

1 个答案:

答案 0 :(得分:0)

检查包名是否正确,删除以下行

MyInput.y1

还要确保你在清单中添加了这些内容:

app:layout_behavior="@string/appbar_scrolling_view_behavior"
 tools:context="app.devbyzero.net.pascasarjanatrisakti.MapActivity"