草庐IT

Android 将 Maps v2 API 添加到主详细信息流 - 错误膨胀类 fragment

coder 2023-12-10 原文

我正在尝试使用第 2 版 API 将 Google map View 添加到内置的 Android Master Detail 流程中。出于某种原因,我收到一个错误膨胀类 fragment 异常:

    12-29 22:47:05.828: E/AndroidRuntime(1655): FATAL EXCEPTION: main
12-29 22:47:05.828: E/AndroidRuntime(1655): android.view.InflateException: Binary XML file line #48: Error inflating class fragment
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at com.reading.trackitparent.MapDetailFragment.onCreateView(MapDetailFragment.java:52)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.os.Handler.handleCallback(Handler.java:605)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.os.Looper.loop(Looper.java:137)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.app.ActivityThread.main(ActivityThread.java:4424)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at java.lang.reflect.Method.invokeNative(Native Method)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at java.lang.reflect.Method.invoke(Method.java:511)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at dalvik.system.NativeStart.main(Native Method)
12-29 22:47:05.828: E/AndroidRuntime(1655): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
12-29 22:47:05.828: E/AndroidRuntime(1655):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)

我创建了一个新的 MapDetailFragment:

package com.reading.trackitparent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.reading.trackitparent.dummy.DummyContent;

/**
 * A fragment representing a single Item detail screen. This fragment is either
 * contained in a {@link ItemListActivity} in two-pane mode (on tablets) or a
 * {@link ItemDetailActivity} on handsets.
 */
public class MapDetailFragment extends Fragment {
    /**
     * The fragment argument representing the item ID that this fragment
     * represents.
     */
    public static final String ARG_ITEM_ID = "item_id";

    /**
     * The dummy content this fragment is presenting.
     */
    private DummyContent.DummyItem mItem;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public MapDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments().containsKey(ARG_ITEM_ID)) {
            // Load the dummy content specified by the fragment
            // arguments. In a real-world scenario, use a Loader
            // to load content from a content provider.
            mItem = DummyContent.ITEM_MAP.get(getArguments().getString(
                    ARG_ITEM_ID));
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_map_detail,
                container, false);

//      // Show the dummy content as text in a TextView.
//      if (mItem != null) {
//          ((TextView) rootView.findViewById(R.id.item_detail))
//                  .setText(mItem.content);
//      }

        return rootView;
    }
}

我正在尝试使用以下布局:

<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=".ItemDetailActivity" >

    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

项目列表 Activity :

package com.reading.trackitparent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

/**
 * An activity representing a list of Items. This activity has different
 * presentations for handset and tablet-size devices. On handsets, the activity
 * presents a list of items, which when touched, lead to a
 * {@link ItemDetailActivity} representing item details. On tablets, the
 * activity presents the list of items and item details side-by-side using two
 * vertical panes.
 * <p>
 * The activity makes heavy use of fragments. The list of items is a
 * {@link ItemListFragment} and the item details (if present) is a
 * {@link ItemDetailFragment}.
 * <p>
 * This activity also implements the required {@link ItemListFragment.Callbacks}
 * interface to listen for item selections.
 */
public class ItemListActivity extends FragmentActivity implements
        ItemListFragment.Callbacks {

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;

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

        if (findViewById(R.id.item_detail_container) != null) {
            // The detail container view will be present only in the
            // large-screen layouts (res/values-large and
            // res/values-sw600dp). If this view is present, then the
            // activity should be in two-pane mode.
            mTwoPane = true;

            // In two-pane mode, list items should be given the
            // 'activated' state when touched.
            ((ItemListFragment) getSupportFragmentManager().findFragmentById(
                    R.id.item_list)).setActivateOnItemClick(true);
        }

        // TODO: If exposing deep links into your app, handle intents here.
    }

    /**
     * Callback method from {@link ItemListFragment.Callbacks} indicating that
     * the item with the given ID was selected.
     */
    @Override
    public void onItemSelected(String id) {
        if (mTwoPane) {
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            Bundle arguments = new Bundle();
            arguments.putString(MapDetailFragment.ARG_ITEM_ID, id);
            MapDetailFragment fragment = new MapDetailFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.item_detail_container, fragment).commit();

        } else {
            // In single-pane mode, simply start the detail activity
            // for the selected item ID.
            Intent detailIntent = new Intent(this, ItemDetailActivity.class);
            detailIntent.putExtra(MapDetailFragment.ARG_ITEM_ID, id);
            startActivity(detailIntent);
        }
    }
}

我引用了 Google Play 服务库。我的 list 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.reading.trackitparent"
    android:versionCode="1"
    android:versionName="1.0" >

    <permission
        android:name="com.reading.trackitparent.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.reading.trackitparent.permission.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.reading.trackitparent.ItemListActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.reading.trackitparent.ItemDetailActivity"
            android:label="@string/title_item_detail" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".ItemListActivity" />
        </activity>

        <uses-library android:name="com.google.android.maps" />
    </application>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxx" />

</manifest>

如果有任何帮助,我将不胜感激!

最佳答案

MapFragment 替换为 SupportMapFragmentSupportMapFragment 是与 fragment 的 Android 支持包反向移植一起使用的。 MapFragment 用于原生 API 级别 11 版本的 fragment 。

关于Android 将 Maps v2 API 添加到主详细信息流 - 错误膨胀类 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14086653/

有关Android 将 Maps v2 API 添加到主详细信息流 - 错误膨胀类 fragment的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  3. ruby-on-rails - ActionController::RoutingError: 未初始化常量 Api::V1::ApiController - 2

    我有用于控制用户任务的Rails5API项目,我有以下错误,但并非总是针对相同的Controller和路由。ActionController::RoutingError:uninitializedconstantApi::V1::ApiController我向您描述了一些我的项目,以更详细地解释错误。应用结构路线scopemodule:'api'donamespace:v1do#=>Loginroutesscopemodule:'login'domatch'login',to:'sessions#login',as:'login',via::postend#=>Teamroutessc

  4. ruby - 可以通过多少种方法将方法添加到 ruby​​ 对象? - 2

    当谈到运行时自省(introspection)和动态代码生成时,我认为ruby​​没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby​​的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资

  5. 【鸿蒙应用开发系列】- 获取系统设备信息以及版本API兼容调用方式 - 2

    在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList​()Obt

  6. ruby-on-rails - Mandrill API 模板 - 2

    我正在使用Mandrill的RubyAPIGem并使用以下简单的测试模板:testastic按照Heroku指南中的示例,我有以下Ruby代码:require'mandrill'm=Mandrill::API.newrendered=m.templates.render'test-template',[{:header=>'someheadertext',:main_section=>'Themaincontentblock',:footer=>'asdf'}]mail(:to=>"JaysonLane",:subject=>"TestEmail")do|format|format.h

  7. ruby-on-rails - 在 Ruby (on Rails) 中使用 imgur API 获取图像 - 2

    我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path

  8. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  9. ruby-on-rails - 使用 HTTParty 的非常基本的 Rails 4.1 API 调用 - 2

    Rails相对较新。我正在尝试调用一个API,它应该向我返回一个唯一的URL。我的应用程序中捆绑了HTTParty。我已经创建了一个UniqueNumberController,并且我已经阅读了几个HTTParty指南,直到我想要什么,但也许我只是有点迷路,真的不知道该怎么做。基本上,我需要做的就是调用API,获取它返回的URL,然后将该URL插入到用户的数据库中。谁能给我指出正确的方向或与我分享一些代码? 最佳答案 假设API为JSON格式并返回如下数据:{"url":"http://example.com/unique-url"

  10. ruby-on-rails - 是否使用 API - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我的公司有一个巨大的数据库,该数据库接收来自多个来源的(许多)事件,用于监控和报告目的。到目前为止,数据中的每个新仪表板或图形都是一个新的Rails应用程序,在巨大的数据库中有额外的表,并且可以完全访问数据库内容。最近,有一个想法让外部(不是我们公司,而是姊妹公司)客户访问我们的数据,并且决定我们应该公开一个只读的RESTfulAPI来查询我们的数据。我的观点是-我们是否也应该为我们的自己

随机推荐