2014-01-13 51 views
0

在我的Android項目,我有一個Service類:WakefulBroadcastReceiver得到拋出java.lang.ClassNotFoundException

public class MyService extends Service{ 
    ... 

    //Defined a WakefulBroadcastReceiver as a inner static class 
    public static class DeviceRebootReceiver extends WakefulBroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
       //Do something when user reboot the device 
     } 
    } 

} 

正如你看到的上面,我在MyService類中定義的WakefulBroadcastReceiver內部靜態類。當用戶重新啓動他/她的設備時,該接收器接收廣播。

我的AndroidManifest.xml是這樣的:

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

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

    <application 
    ...> 
     <activity ...> ... </activity> 

     <service android:name="com.my.project.MyService" 
       android:exported="false"/> 

     <receiver 
      android:name="com.my.project.MyService$DeviceRebootReceiver" 
      > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 
    </application> 
</manifest> 

但是當我運行重啓設備後,我的應用程序&,我logcat的顯示我的錯誤:

Unable to instantiate receiver com.my.project.MyService$DeviceRebootReceiver: 
java.lang.ClassNotFoundException: com.my.project.MyService$DeviceRebootReceiver in loader dalvik.system.PathClassLoader[/data/app/com.my.project-1.apk] 

爲什麼我的接收機類是不能夠被系統加載?

=====更新======

我也試圖讓我的DeviceRebootReceiver單獨類(DeviceRebootReceiver.java),並提出了AndroidManifest.xml改爲:

<application...> 
    <receiver 
     android:name="com.my.project.DeviceRebootReceiver" 
      > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
    </receiver> 
</application> 

但我仍然得到了logcat中類似的錯誤:

java.lang.RuntimeException: Unable to instantiate receiver com.my.project.DeviceRebootReceiver: java.lang.ClassNotFoundException 
+0

是您的服務在重啓設備時運行嗎?可能它;因爲你的服務在重啓時死了,所以子類無法到達,沒有發現異常! – Saqib

+0

@Saqib,我也試圖把我的DeviceRebootReceiver放在它自己的類文件中,但是類似的錯誤仍然在增加。請檢查我的更新。 –

+0

嗯,你有沒有嘗試加入'android:permission =「android.permission.RECEIVE_BOOT_COMPLETED」' – Saqib

回答

3

我有同樣的問題,並解決它。

這是關於Android的支持,v4.jar庫,你應該只在官方和更新庫項目和Android的私人libs文件夾下(從ANDROID_SDK /演員/安卓/支持/複印)在您的項目的Java Build Path中檢查到的庫。舉例來說,我也有過,因爲我的項目是使用另一個庫(ActionBarSherlock)的Android的支持,v4.jar甚至包括在我的Java構建Android官方支持,v4.jar的ClassNotFoundException的路徑,所以我沒有選中ActionBarSherlock項目的Android專用庫以避免此冗餘。

相關問題