2012-11-09 167 views
1

我正在寫一個簡單的代碼,當我收到消息時啓動廣播接收器。但是我收到了一個強制關閉錯誤。我在AndroidManifest.xml中添加了必需的權限。我從本網站獲取了BroadcastReceiever的代碼(http://www.androidcompetencycenter.com/2008/12/android-api-sms-handling/)。請告訴我我要出錯的地方。這裏是我的代碼:廣播接收器啓動錯誤

package com.example.findme; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.SmsMessage; 
import android.widget.Toast; 



public class SMSReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Bundle bundle = intent.getExtras(); 

     Object messages[] = (Object[]) bundle.get("pdus"); 
     SmsMessage smsMessage[] = new SmsMessage[messages.length]; 
     for (int n = 0; n < messages.length; n++) { 
      smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);*/ 
     Toast.makeText(context,"Reveived!",1).show(); 
     } 

     // show first message 
     Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG); 
     toast.show(); 
    } 

這裏是logcat的:

11-09 21:05:03.190: D/AndroidRuntime(446): Shutting down VM 
11-09 21:05:03.190: W/dalvikvm(446): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
11-09 21:05:03.230: E/AndroidRuntime(446): FATAL EXCEPTION: main 
11-09 21:05:03.230: E/AndroidRuntime(446): java.lang.RuntimeException: Unable to instantiate receiver com.example.smsmanager1.receiver.SMSReceiver: java.lang.ClassNotFoundException: com.example.smsmanager1.receiver.SMSReceiver in loader dalvik.system.PathClassLoader[/data/app/com.example.smsmanager1-1.apk] 
11-09 21:05:03.230: E/AndroidRuntime(446): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2789) 
11-09 21:05:03.230: E/AndroidRuntime(446): at android.app.ActivityThread.access$3200(ActivityThread.java:125) 
11-09 21:05:03.230: E/AndroidRuntime(446): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083) 
11-09 21:05:03.230: E/AndroidRuntime(446): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-09 21:05:03.230: E/AndroidRuntime(446): at android.os.Looper.loop(Looper.java:123) 
11-09 21:05:03.230: E/AndroidRuntime(446): at android.app.ActivityThread.main(ActivityThread.java:4627) 
11-09 21:05:03.230: E/AndroidRuntime(446): at java.lang.reflect.Method.invokeNative(Native Method) 
11-09 21:05:03.230: E/AndroidRuntime(446): at java.lang.reflect.Method.invoke(Method.java:521) 
11-09 21:05:03.230: E/AndroidRuntime(446): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
11-09 21:05:03.230: E/AndroidRuntime(446): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
11-09 21:05:03.230: E/AndroidRuntime(446): at dalvik.system.NativeStart.main(Native Method) 
11-09 21:05:03.230: E/AndroidRuntime(446): Caused by: java.lang.ClassNotFoundException: com.example.smsmanager1.receiver.SMSReceiver in loader dalvik.system.PathClassLoader[/data/app/com.example.smsmanager1-1.apk] 
11-09 21:05:03.230: E/AndroidRuntime(446): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243) 
11-09 21:05:03.230: E/AndroidRuntime(446): at java.lang.ClassLoader.loadClass(ClassLoader.java:573) 
11-09 21:05:03.230: E/AndroidRuntime(446): at java.lang.ClassLoader.loadClass(ClassLoader.java:532) 
11-09 21:05:03.230: E/AndroidRuntime(446): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2780) 
11-09 21:05:03.230: E/AndroidRuntime(446): ... 10 more 
11-09 21:05:13.211: I/Process(446): Sending signal. PID: 446 SIG: 9 

回答

2

類全名是在接收器的聲明不正確。

在你的清單文件,則必須更換

com.example.smsmanager1.receiver.SMSReceiver 

通過

com.example.findme.SMSReceiver 
+0

萬分感謝!有效! – Saturnian

+0

不用客氣:) – fiddler

+0

如果你的接收器是BOOT_COMPLETED的話,這個更加難以發現,因爲你沒有得到logCat –