繁体   English   中英

Android:错误放大类片段

[英]Android : Error inflating class fragment

XML布局文件

<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"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    android:paddingLeft="@dimen/activity_horizontal_margin"
                    android:paddingRight="@dimen/activity_horizontal_margin"
                    android:paddingTop="@dimen/activity_vertical_margin"
                    tools:context="com.gaurav.googlemap.HomeMap" >

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

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

</RelativeLayout>

Java文件

package com.beproject.ourway;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

public class HomeMap extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_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.home_map, 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);
}
}

在我之前也搜索此问题时,我已经做了以下事情来解决

1.进口以下课程

import android.support.v4.app.Fragment;  
import android.support.v4.app.FragmentActivity;

2.扩展的FragmentActivity

我仍然在logcat中收到此错误

Error inflating class fragment

还有其他解决方法吗? 谢谢。

假设您已使用“支持片段”,则需要用SupportMapFragment替换MapFragment 这样做应该可以解决它。

采用:

class="com.google.android.gms.maps.SupportMapFragment"

代替 :

android:name="com.google.android.gms.maps.MapFragment"

因此您的xml必须像这样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM