2011-11-17 37 views
29

我正在嘗試來自this blog的應用程序。同時延長了FragmentActivity,我發現了以下錯誤:FragmentActivity無法解析爲類型

`FragmentActivity` was not able to resolve. 

我錯過任何圖書館或別的什麼嗎?

我的代碼:

public class Testing_newActivity extends FragmentActivity { // here the FragmentActivity getting error package not found for import 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.main); 
     if (getResources().getConfiguration().orientation 
       == Configuration.ORIENTATION_LANDSCAPE) { 
      // If the screen is now in landscape mode, we can show the 
      // dialog in-line so we don't need this activity. 
      finish(); 
      return; 
     } 

     if (savedInstanceState == null) { 
      // During initial setup, plug in the details fragment. 
      DetailsFragment details = new DetailsFragment(); 
      details.setArguments(getIntent().getExtras()); 

      getSupportFragmentManager().beginTransaction().add(
        android.R.id.content, details).commit(); 
     }  
    } 
} 

Android清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.searce.testingnew" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="11" /> 

    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > 
     <activity android:label="@string/app_name" android:name=".Testing_newActivity" > 
      <intent-filter > 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

通過您的進口,並確保所有需要的都在那裏。 (如果你發佈了一些代碼/錯誤日誌/有些東西會更容易幫助)。 – kaspermoerch

回答

45

經過長時間的等待這個答案,我已經整理了一些東西,我認爲很多其他人需要一個簡單的解決方案來解決這個問題。

警告:該解決方案適用於使用Indigo或更低版本的eclipse用戶。仍在尋找使用其他IDE for android的解決方案用戶。

當延長FragmentActivity對於具有2.2或更低版本的應用程序,你可以從here下載android.supportv4.jar或者你可以從你的Android SDK目錄..\android-sdk\extras\android\support\v4得到這個投入項目。

如果您的項目中沒有目錄庫,請創建libs目錄並將android.supportv4.jar文件粘貼到此目錄中。

從eclipse項目工作區: 請選擇您想要使用的project/application。右鍵單擊Project並選擇Properties選項。在這個選擇Java build path和選擇選項卡Libraries

enter image description here

現在點擊添加罐。它將顯示選定當前項目的當前項目列表。

展開此操作並轉到libs目錄並選擇android.supportv4.jar文件,然後單擊確定。它現在將顯示在列表框中。 Remember add the jar file as relative path path not as absolute path, so whenever if you change the directory of your project or open in another machine then will detect automatically for the project directory.現在點擊確定關閉屬性對話框。

將鼠標懸停在FragmentActivity上或按下ctrl + shift + o導入缺失的文件,即可導入android.support.v4.app.FragmentActivity;

希望你現在可以得到FragmentActivity類。

如果您使用的是eclipse juno,那麼無需擔心下載支持jar文件。它會自動放入libs目錄並自動添加到您的項目路徑中。如果沒有的話,請通過Properties選項並添加它。

一件事如果您需要支持您的應用程序更低的版本,然後建立與更高版本和該行添加到您的AndroidManifest.xml文件

<uses-sdk 
    android:minSdkVersion="3" 
    android:targetSdkVersion="15" /> 

這將支持1.5〜4.0。 1版本設備。

+0

這對我有幫助!不過,我按**添加外部JAR ... ** – ymerdrengene

1

FragmentActivity是兼容性庫的一部分,而不是Android 3.0的SDK。 除非要向後兼容,否則不需要使用它。

不知道爲什麼這裏提到這可能是它的一個項目特定的類。

試着看一下附帶的SDK HCGallery樣品

+0

那麼哪個庫就是爲了這個? – Pratik

5

你有外部的Android支持jar例如添加到您的類路徑在我的情況下,我添加了android-support-v4.jar,它通常位於{android_home}\extras\android\support\v4目錄中。我使用的是Windows XP,在我的情況下,它位於C:\Program Files\Android\android-sdk\extras\android\support\v4

希望這有助於任何人尋找答案。

38

如果你使用的是eclipse和windows,下面是你如何擺脫這個錯誤。

Right click on your project folder->Build path-> Configure build path-> 
Add External Jars -> select "android-support-v4.jar" file (It'll be located in Android "android-sdk-windows\extras\android\support") 
then click OK. 
+0

那麼WatchActivity不能解決的類型 – Prasad