-1
public class MainActivity extends Activity {

private ArrayList<BroadcastReceiver> _senders;
private IMsgSender _sender;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // xml....
    _sender = new HTTPMsgSender("http://localhost:8080/SyncDroidServer/AndroidServlet", "user", "12345");

    _senders = new ArrayList<BroadcastReceiver>();
    _senders.add(new SmsListener(_sender));

//////////////////////



public class SmsListener extends BroadcastReceiver {

    private IMsgSender _messageSender;

    public SmsListener(IMsgSender messageSender) {
        super();
        this._messageSender = messageSender;
    }    
@Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        Toast.makeText(context, "goooodddddddd", Toast.LENGTH_SHORT).show();
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";            
        if (bundle != null)
        {
            SyncSmsMessage smsMessage = new  SyncSmsMessage();
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                str += "SMS from " + msgs[i].getOriginatingAddress();                     
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";        
                Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
                smsMessage.Id = 1; // produce a uniqe id
                smsMessage.ReceiveDate = null; // put the current date
                smsMessage.MessageText = msgs[i].getMessageBody().toString(); // get the string from the sms
                smsMessage.SenderNum = msgs[i].getOriginatingAddress();

                _messageSender.SendMsg(smsMessage);
            }
    /////////////////////
    public interface IMsgSender 
    {
    public void SendMsg(MessageBase messageToSend);
    }
    ///////////////////////
    public class HTTPMsgSender  implements IMsgSender{


        @Override
        public void SendMsg(MessageBase messageToSend) 
        {
        }
    }

LogCat 显示

    03-10 10:30:41.902: E/AndroidRuntime(878): FATAL EXCEPTION: main
03-10 10:30:41.902: E/AndroidRuntime(878): java.lang.RuntimeException: Unable to start receiver com.example.syncapp.SmsListener: java.lang.NullPointerException
03-10 10:30:41.902: E/AndroidRuntime(878):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
03-10 10:30:41.902: E/AndroidRuntime(878):  at android.app.ActivityThread.access$2400(ActivityThread.java:117)
03-10 10:30:41.902: E/AndroidRuntime(878):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
03-10 10:30:41.902: E/AndroidRuntime(878):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 10:30:41.902: E/AndroidRuntime(878):  at android.os.Looper.loop(Looper.java:123)
03-10 10:30:41.902: E/AndroidRuntime(878):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-10 10:30:41.902: E/AndroidRuntime(878):  at java.lang.reflect.Method.invokeNative(Native Method)
03-10 10:30:41.902: E/AndroidRuntime(878):  at java.lang.reflect.Method.invoke(Method.java:507)
03-10 10:30:41.902: E/AndroidRuntime(878):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-10 10:30:41.902: E/AndroidRuntime(878):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-10 10:30:41.902: E/AndroidRuntime(878):  at dalvik.system.NativeStart.main(Native Method)
03-10 10:30:41.902: E/AndroidRuntime(878): Caused by: java.lang.NullPointerException
03-10 10:30:41.902: E/AndroidRuntime(878):  at com.example.syncapp.SmsListener.onReceive(SmsListener.java:59)
03-10 10:30:41.902: E/AndroidRuntime(878):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
03-10 10:30:41.902: E/AndroidRuntime(878):  ... 10 more

当我在主要活动中这样做时

public class MainActivity extends Activity {

private ArrayList<BroadcastReceiver> _senders;
private IMsgSender _sender;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // xml....
    _sender = new HTTPMsgSender("http://localhost:8080/SyncDroidServer/AndroidServlet", "user", "12345");

    SyncSmsMessage s= new SyncSmsMessage();
    s.Id=1;
    s.MessageText="sfsa";
    _sender.SendMsg(s);

所有代码都在 onReceive 方法的最后一行运行,并且应用程序崩溃了。(_messageSender.SendMsg(smsMessage);) 在 SendMsg 中我什么也不做。我需要做什么???

java.lang.RuntimeException:无法启动接收器 com.example.syncapp.SmsListener:java.lang.NullPointerException

4

2 回答 2

0

我觉得有两种可能

  1. _sender 要么为空(可能性较小)..
  2. 在创建 SmsListener 之前调用 onReceive。(有机会吗?)。

在 SmsListener 构造函数中写一个日志,看它是否被正确调用。在添加到 arraylist 之前还要检查 _sender 是否为空

于 2013-03-10T12:23:23.730 回答
0

因为 onrecive 是系统服务,所以 soulutin 与 Intents 一起使用。

于 2013-03-10T22:27:33.403 回答