2012-12-26 32 views
11

我在這裏找到了關於這個主題的幾個問題,但他們的答案似乎並不適用,因爲我的項目引用了包含該類的庫。這裏是我的代碼:Google maps api v2 - 類型android.app.Fragment無法解析。它是從所需的.class文件間接引用的。

package com.demo.app; 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.view.ViewPager; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class Mapview extends FragmentActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     GPSTracker gps = new GPSTracker(Mapview.this); 

     // check if GPS enabled  
     if(gps.canGetLocation()){ 

      double latitude = gps.getLatitude(); 
      double longitude = gps.getLongitude(); 

      // \n is for new line 
      // Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
     }else{ 
      // can't get location 
      // GPS or Network is not enabled 
      // Ask user to enable GPS/network in settings 
      gps.showSettingsAlert(); 
     } 

     double latitude = gps.getLatitude(); 
     double longitude = gps.getLongitude(); 


     SupportMapFragment fragment = new SupportMapFragment(); 
     getSupportFragmentManager().beginTransaction() 
       .add(android.R.id.content, fragment).commit(); 

     GoogleMap mMap; 
     mMap = ((MapFragment) getSupportFragmentManager().findFragmentById(android.R.id.content)).getMap(); 
     mMap.addMarker(new MarkerOptions() 
       .position(new LatLng(0, 0)) 
       .title("Hello world")); 


    } 
} 

所以你可以看到,我在進口中引用的片段庫時,Eclipse不引發錯誤,因此似乎承認它的存在。在下面的圖片中,您可以看到我的eclipse項目顯示了參考庫。在這裏你可以看到帶有findFragmentById()的FragmentManager類。我還以類似的方式找到了我參考的其他課程。因此,總結我有一個lib文件夾中的庫,並且它們通過eclipse進行確認,並且在代碼中沒有發生錯誤,但是由於錯誤,程序包不會導出,唯一的錯誤是在此代碼的第一行旁邊的「 p「在包中說明:'類型android.app.Fragment無法解析。它是間接需要的.class文件

referenced libraries

任何想法引用?

我嘗試清理和重建幾次路徑。

回答

27

嘗試改變

mMap = ((MapFragment) getSupportFragmentManager().findFragmentById(android.R.id.content)).getMap(); 

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(android.R.id.content)).getMap(); 

您必須運行Android的預ICS版本,並且您在使用「MapFragment」拉內置片段的版本,而不是支持庫版本。

+0

這樣做。總是小事。 –

3

確保您的目標SDK爲3.0或以上。

在Eclipse中,單擊項目菜單,單擊屬性,單擊左側的Android,選擇3.0或更高版本的目標SDK。

相關問題