2013-04-02 115 views
4

我的活動代碼

package com.example.testmap; 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.view.Menu; 
import com.google.android.gms.maps.SupportMapFragment; 

public class MainActivity extends FragmentActivity { 

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

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

} 

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


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

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

</RelativeLayout> 

和清單代碼

<?xml version="1.0" encoding="utf-8"?> 

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

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

<permission 
    android:name="android.os.Bundle.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<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-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" /> 

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

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

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

,但我發現錯誤

FATAL EXCEPTION: main 
java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable 
at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source) 
at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source) 
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:279) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:408) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209) 
at android.app.Activity.setContentView(Activity.java:1657) 
at com.example.testmap.MainActivity.onCreate(MainActivity.java:13) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:130) 
at android.app.ActivityThread.main(ActivityThread.java:3687) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
at dalvik.system.NativeStart.main(Native Method) 

回答

7

嘗試刪除這一行:

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

從XML文件中的片段,它不應該是那裏。

2.更改此權限:

<permission 
android:name="android.os.Bundle.permission.MAPS_RECEIVE" 
android:protectionLevel="signature" /> 

這樣:

<permission 
android:name="com.example.mapdemo.permission.MAPS_RECEIVE" 
android:protectionLevel="signature" /> 

如果com.example.mapdemo是你的項目包。 如果不將其更改爲你的項目包在此權限,這:

<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" /> 
+0

點二號解決我的問題 –

+0

大@androiddeveloper,有一個很好的編碼:) –

0

嘗試從Android SDK中導入項目: /演員/谷歌/ google_play_services_libproject/google_play_services_lib 到您的工作空間在內部消除您的應用程序添加任何文件夾/ lib目錄庫。

做到這一點後,將它們添加在您的:項目>屬性>安卓>庫添加

+0

您使用的是片段或活動? – lucasddaniel