-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

私がメインアクティビティitsworksでこれを行うとき

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

onrecive はシステム サービスであるため、soulutin は Intents で使用します。

于 2013-03-10T22:27:33.403 に答える
0

2つの可能性があると思います

  1. _sender が null の場合 (可能性は低い) ..
  2. onReceive は、SmsListener が作成される前に呼び出されます。(その可能性はありますか?)。

SmsListener コンストラクターにログを書き込んで、正しく呼び出されているかどうかを確認します。また、arraylist に追加する前に _sender が null かどうかを確認します

于 2013-03-10T12:23:23.730 に答える