4

我正在構建一個簡單的Hello World應用,以瞭解Android兼容包。我能夠獲得該應用在3.2模擬器上運行,但是當我在2.3.3模擬器中運行它,我得到無法解析在Android 2.3.3上運行的具有兼容性包v4的FragmentActivity

10-12 11:36:14.474: WARN/dalvikvm(469): Unable to resolve superclass of Lcom/example/MyActivity; (11) 
10-12 11:36:14.564: WARN/dalvikvm(469): Link of class 'Lcom/example/MyActivity;' failed 
10-12 11:36:14.564: DEBUG/AndroidRuntime(469): Shutting down VM 
10-12 11:36:14.584: WARN/dalvikvm(469): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
10-12 11:36:14.624: ERROR/AndroidRuntime(469): FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example/com.example.MyActivity}: java.lang.ClassNotFoundException: com.example.MyActivity in loader dalvik.system.PathClassLoader[/data/app/com.example-1.apk] 

所以,很顯然它無法找到FragmentActivity(這是COM的超級。 example.MyActivity)。我只是不知道爲什麼。

需要注意以下幾點:

1)我在之後的http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/教程這是不是很徹底。

2)我很確定我正在用maven正確地將兼容性包構建到APK中。我在我的本地maven倉庫中安裝了jar,並且依靠它來編譯。我認爲如果我沒有正確地構建它,它將不會在3.2模擬器上運行。

3)我試過用IntelliJ和maven-compiler-plugin構建。同樣的結果。

任何幫助將不勝感激。謝謝。

編輯... 這裏的清單

<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="11" /> 
<uses-permission android:name="android.permission.INTERNET" /> 

<application android:label="@string/app_name" android:icon="@drawable/icon"> 
    <activity android:name=".MyActivity" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".TutViewerActivity" 
       android:label="@string/app_name" > 
    </activity> 
</application> 
<uses-sdk android:minSdkVersion="7" /> 

和MyActivity定義

public class MyActivity extends FragmentActivity implements TutListFragment.OnTutSelectedListener 

回答

4

我有同樣的問題,這個問題是兼容包不正確包括在內。該例外是不明確的,因爲它說它找不到MyActivity,但完整的堆棧跟蹤將顯示它無法鏈接的FragmentActivity。檢查範圍,並確保正確的版本在您的存儲庫中,並且確實在構建時包含它。

如果您仍然遇到此問題,請嘗試在此處包含您的pom.xml。

+5

我也有問題。我發現你的評論,丹,並按照你的說法。右鍵單擊項目,Android工具,添加支持庫。訣竅! – Davek804

+0

謝謝Davek804,這麼多日子裏我一直很生氣。不知道ADT團隊是否發佈了這個特定事情的官方聲明! –

+0

是的,看看這個[回答](http:// stackoverflow。com/questions/11764195/unable-to-resolve-superclass-of-landroid-support-v4-app-fragmentactivity)。在導出構建路徑中未選中的私有依賴可能是一個可能的問題。 – asgs

1

我有同樣的問題,兼容性包沒有正確內置到apk中。爲了實現這一目標,您可以使用以下步驟:

developer.android.com:

要添加的圖書館之一到你的Android項目:

In your Android project, create a directory named libs at the root of your project (next to src/, res/, etc.) 
Locate the JAR file for the library you want to use and copy it into the libs/ directory. 

For example, the library that supports API level 4 and up is located at <sdk>/extras/android/support/v4/android-support-v4.jar. 
Add the JAR to your project build path. 

In Eclipse, right-click the JAR file in the Package Explorer, select Build Path > Add to Build Path. 
0

添加以下MyActivity進口:

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 

即使您沒有使用這些類別秒。

相關問題