2

祝大家新年快乐,

在 Android 中实现 Fragments 的新功能(我看过类似的帖子,但我无法为我的问题找到答案),所以这里是:

我希望使用 SupportMapFragment(从支持库 v4 引用)并且我创建了一个扩展 FragmentActivity 的类,称为 TripSummary.java:

package com.project.locationapp;

import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class TripSummary extends android.support.v4.app.FragmentActivity {

private GoogleMap mMap;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = new Intent(this, LocationService.class);
    stopService(intent);
    setContentView(R.layout.activity_trip_summary);

    createMap();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_trip_summary, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //Content?
    return super.onOptionsItemSelected(item);
}

private void createMap(){
    //if a map has not already been instantiated it'll return null
    if(mMap == null){
        //instantiate map
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        //check it has been instantiated
        if(mMap != null){
            Toast.makeText(this, "map done", Toast.LENGTH_SHORT)
            .show();
            //Manipulate map here (add coordinates/polylines from trip etc etc.)
        }
    }
}

}

从 activity_trip_summary.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" >

<TextView
    android:id="@+id/tripSummary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/trip_summary"
    tools:context=".Start" />

<TextView
    android:id="@+id/tripDetails"
    android:layout_below="@id/tripSummary"
    android:layout_alignParentLeft="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/awaiting_location"
    tools:context=".Start" />

<Fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:layout_below="@id/tripDetails"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

AndroidManifest.xml 如下:

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<permission
      android:name="com.project.locationapp.permission.MAPS_RECEIVE"
      android:protectionLevel="signature"/>
<uses-permission android:name="com.project.locationapp..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_FINE_LOCATION" />
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>


<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.project.locationapp.Start"
        android:label="@string/title_activity_start" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name="com.project.locationapp.LocationService" />

    <activity
        android:name="com.project.locationapp.TripSummary"
        android:label="@string/title_activity_trip_summary" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.project.locationapp.Start" />
    </activity>
    <activity
        android:name="com.project.locationapp.ViewTrips"
        android:label="@string/title_activity_view_trips" >
    </activity>
    <activity
        android:name="com.project.locationapp.TripDetail"
        android:label="@string/title_activity_trip_detail" >
    </activity>
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="<-- My Key -->"/>
</application>

</manifest>

每次我尝试加载活动 TripSummary 时,我遇到的问题是“对不起!意外停止”对话框,应用程序崩溃。

日志猫:

java.lang.RuntimeException: Unable to start activity                   ComponentInfo{com.project.locationapp/com.project.locationapp.TripSummary}:     android.view.InflateException: Binary XML file line #24: Error inflating class Fragment

我正在引用 google-play-services_lib 库(也在我的工作区中),并且在我的项目的 /libs 目录中也有一个 android-support-v4.jar 的副本。

如果您知道我的问题的答案,请分享!

提前致谢。

4

1 回答 1

1

请更换:

<Fragment

和:

<fragment

此外,您可以摆脱该元素中多余/不正确的命名空间声明。

另外,将来,发布完整的堆栈跟踪,而不仅仅是一行的一部分,以便人们更容易地帮助你。

于 2013-01-01T20:35:45.670 回答