2013-11-20 55 views
0

我想向用戶展示TabActivity上的地圖。選項卡上沒有錯誤。我有4個tabActivities,其中一個將包含一張地圖。實現谷歌地圖片段的錯誤

我的佈局文件;

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <fragment 
     android:id="@+id/myMapView" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:clickable="true" 
     /> 
</RelativeLayout> 

My Class file with imports;

package com.mypackage; 

import android.graphics.Canvas; 
import android.graphics.RectF; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.Fragment; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.UiSettings; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapController; 

public class Main extends FragmentActivity{ 

private GoogleMap mMap; 

UiSettings settings; 
MapController mapController; 
GeoPoint geopoint; 
RectF oval; 
Canvas canvas; 
int mRadius = 5; 

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

    setUpMap(); 
} 

@Override 
protected void onResume(){ 
    super.onResume(); 
    setUpMap(); 
} 

private void setUpMap(){ 
    if (mMap != null) { 
     return; 
    } 
    mMap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView)).getMap(); 
    if (mMap == null) { 
     return; 
    } 
    final LatLng place1 = new LatLng(40,40); 

    mMap.addMarker(new MarkerOptions() 
    .position(place1) 
    .title("blabla") 
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); 

    mMap.getUiSettings().setCompassEnabled(true); 
    mMap.getUiSettings().setZoomControlsEnabled(true); 
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place1, 15)); 
    mMap.setTrafficEnabled(true); 
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
} 
} 

而我的清單文件(僅關於mapclass和mapsupport);

<permission 
     android:name="com.mypackage.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature"/> 
<uses-permission android:name="com.mypackage.permission.MAPS_RECEIVE"/> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 


<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="myApiKey"/> 

    <activity 
     android:name="SplashScreen" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="Main" 
     android:label="@string/app_name" 
     android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"> 
    </activity> 
</application> 

編輯: 最後logcat的輸出;

11-21 08:34:54.179: E/AndroidRuntime(27297): FATAL EXCEPTION: main 
11-21 08:34:54.179: E/AndroidRuntime(27297): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage/com.mypackage.Main}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1499) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:676) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.widget.TabHost.setCurrentTab(TabHost.java:348) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:141) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.view.View.performClick(View.java:2485) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.view.View$PerformClick.run(View.java:9080) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.os.Handler.handleCallback(Handler.java:587) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.os.Looper.loop(Looper.java:130) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.app.ActivityThread.main(ActivityThread.java:3768) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at java.lang.reflect.Method.invokeNative(Native Method) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at java.lang.reflect.Method.invoke(Method.java:507) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at dalvik.system.NativeStart.main(Native Method) 
11-21 08:34:54.179: E/AndroidRuntime(27297): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.view.LayoutInflater.inflate(LayoutInflater.java:408) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.app.Activity.setContentView(Activity.java:1657) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at com.mypackage.Main.onCreate(AdresTab.java:33) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623) 
11-21 08:34:54.179: E/AndroidRuntime(27297): ... 18 more 
11-21 08:34:54.179: E/AndroidRuntime(27297): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.mypackage-1.apk] 
11-21 08:34:54.179: E/AndroidRuntime(27297): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at java.lang.ClassLoader.loadClass(ClassLoader.java:551) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.view.LayoutInflater.createView(LayoutInflater.java:471) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66) 
11-21 08:34:54.179: E/AndroidRuntime(27297): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568) 
11-21 08:34:54.179: E/AndroidRuntime(27297): ... 27 more 

任何其他有用的想法請參閱??

+0

請發佈完整的LogCat輸出。這兩行信息不足,因爲我看不到代碼中有錯誤。 –

回答

0

我解決了我的問題。我的佈局文件中的片段類無法從google_play_services庫中找到。我忘了將這個庫的jar文件添加到我的項目中。我只從java構建路徑添加該項目。我必須從項目 - >屬性 - > Android->庫添加此庫。這解決了。不管怎麼說,還是要謝謝你。

2

使用下面的代碼:

GoogleMap mMap; 
SupportMapFragment mapFrag; 

現在設置:

if (mMap == null) { 
      mapFrag= (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.myMapView); 
      mMap = mapFrag.getMap(); 

      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 
       // The Map is verified. It is now safe to manipulate the map. 
       AddMarkers(); 
      } 
     } 

而且清單文件中添加這個元標記。

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

    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
+0

爲什麼GoogleMap必須是靜態的? –

+0

@RafaelT其實我有同樣的問題,我試過上面的代碼。它沒有必要採取靜態。 –

+0

我應該添加到google_play_services_version和整數元數據?我認爲整數應該是我的apikey? –

相關問題