新年快乐,
在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时,我都会得到一个'抱歉!意外停止'对话框和应用程序崩溃。
logcat的:
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的副本。
如果您知道我的问题的答案,请分享!
提前致谢。
答案 0 :(得分:1)
请替换:
<Fragment
使用:
<fragment
此外,您可以摆脱该元素中的冗余/不正确的名称空间声明。
此外,在将来,发布完整的堆栈跟踪,而不仅仅是一行的一部分,以便人们更容易为您提供帮助。