我想将地图功能添加到我的应用程序中。我按照谷歌上的步骤,在我的清单中添加了所有优秀的员工。这是代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.events.activities"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-permission android:name="com.events.activities.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="MY_API_KEY" />
<activity
android:name="com.events.activities.SplashActivity"
android:label="@string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.events.activities.LoginActivity"
android:label="@string/title_activity_login" >
</activity>
<activity
android:name="com.events.activities.MasterActivity"
android:label="@string/title_activity_master" >
</activity>
</application>
</manifest>
现在我有一个带有选项卡的活动,每个选项卡都是一个片段,我希望其中一个片段显示一张地图(上面涂满了淀粉),所以我创建了这个map_frag.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" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
这是片段类:
public class MapFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.map_frag, container, false);
return rootView;
}
}
但是每次我打开标签时都会发生崩溃,这里是 LogCat:
11-12 06:41:05.387: E/AndroidRuntime(1329): FATAL EXCEPTION: main
11-12 06:41:05.387: E/AndroidRuntime(1329): android.view.InflateException: Binary XML file line #6: Error inflating class fragment
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-12 06:41:05.387: E/AndroidRuntime(1329): at com.events.fragments.MapFragment.onCreateView(MapFragment.java:23)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.app.Fragment.performCreateView(Fragment.java:1695)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.app.BackStackRecord.run(BackStackRecord.java:682)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.os.Handler.handleCallback(Handler.java:725)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.os.Looper.loop(Looper.java:137)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-12 06:41:05.387: E/AndroidRuntime(1329): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 06:41:05.387: E/AndroidRuntime(1329): at java.lang.reflect.Method.invoke(Method.java:511)
11-12 06:41:05.387: E/AndroidRuntime(1329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-12 06:41:05.387: E/AndroidRuntime(1329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-12 06:41:05.387: E/AndroidRuntime(1329): at dalvik.system.NativeStart.main(Native Method)
11-12 06:41:05.387: E/AndroidRuntime(1329): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.support.v4.app.Fragment.instantiate(Fragment.java:409)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
11-12 06:41:05.387: E/AndroidRuntime(1329): ... 19 more
11-12 06:41:05.387: E/AndroidRuntime(1329): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: /data/app/com.events.activities-2.apk
11-12 06:41:05.387: E/AndroidRuntime(1329): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
11-12 06:41:05.387: E/AndroidRuntime(1329): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-12 06:41:05.387: E/AndroidRuntime(1329): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-12 06:41:05.387: E/AndroidRuntime(1329): at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
11-12 06:41:05.387: E/AndroidRuntime(1329): ... 22 more
我不明白为什么我在Binary XML file
.
还有一件事:
我无法将此类导入GoogleMap
我MapFragment
的,如this SO question所示。也许我需要在我的项目中添加一些东西