自定义短信收发器在首次收到短信时崩溃[Android]

时间:2016-03-05 03:42:00

标签: java android android-intent android-activity sms

我写了一个可以接收短信的android应用程序。该应用程序正在运行它可以接收短信,但每次应用程序都是新安装。每次我从模拟器发送短信到应用程序(无需手动打开)它崩溃或强制关闭,但如果我手动打开它可以接收短信没有问题

这是我的代码,如果应用程序接收短信,它将执行此代码

public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    SmsMessage [] msgs = null;
    String messageReceived = "";
    String senderPhoneNumber = null;
    long senderTimestamp = 0;
    String strTimestamp = null;

    if (bundle != null) {

        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]);                
            messageReceived += msgs[i].getMessageBody().toString();
            messageReceived += "\n";
        }

        senderPhoneNumber = msgs[0].getOriginatingAddress ();
        senderTimestamp = msgs[0].getTimestampMillis() / 1000L;
        strTimestamp = senderTimestamp + "";

        if(MainActivity.active){
            MainActivity.populateView(senderPhoneNumber, messageReceived, strTimestamp);
        } else {
            Intent i = new Intent(context,MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            MainActivity.populateView(senderPhoneNumber, messageReceived, strTimestamp);
            context.startActivity(i);
        }

       }

}

请有人帮我这个。我希望不用手动打开它就能收到短信。

修改 这是堆栈跟踪

03-05 11:47:47.208: E/Trace(1512): error opening trace file: No such file or directory (2)
03-05 11:47:47.368: D/AndroidRuntime(1512): Shutting down VM
03-05 11:47:47.368: W/dalvikvm(1512): threadid=1: thread exiting with uncaught exception (group=0xb4e7f288)
03-05 11:47:47.430: E/AndroidRuntime(1512): FATAL EXCEPTION: main
03-05 11:47:47.430: E/AndroidRuntime(1512): java.lang.RuntimeException: Unable to start receiver com.amzngscrum.alpha2.SMSReceiver: java.lang.NullPointerException
03-05 11:47:47.430: E/AndroidRuntime(1512):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2236)
03-05 11:47:47.430: E/AndroidRuntime(1512):     at android.app.ActivityThread.access$1500(ActivityThread.java:130)
03-05 11:47:47.430: E/AndroidRuntime(1512):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
03-05 11:47:47.430: E/AndroidRuntime(1512):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 11:47:47.430: E/AndroidRuntime(1512):     at android.os.Looper.loop(Looper.java:137)
03-05 11:47:47.430: E/AndroidRuntime(1512):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-05 11:47:47.430: E/AndroidRuntime(1512):     at java.lang.reflect.Method.invokeNative(Native Method)
03-05 11:47:47.430: E/AndroidRuntime(1512):     at java.lang.reflect.Method.invoke(Method.java:511)
03-05 11:47:47.430: E/AndroidRuntime(1512):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-05 11:47:47.430: E/AndroidRuntime(1512):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-05 11:47:47.430: E/AndroidRuntime(1512):     at dalvik.system.NativeStart.main(Native Method)
03-05 11:47:47.430: E/AndroidRuntime(1512): Caused by: java.lang.NullPointerException
03-05 11:47:47.430: E/AndroidRuntime(1512):     at com.amzngscrum.alpha2.CustomAdapterr.<init>(CustomAdapterr.java:26)
03-05 11:47:47.430: E/AndroidRuntime(1512):     at com.amzngscrum.alpha2.MainActivity.populateView(MainActivity.java:134)
03-05 11:47:47.430: E/AndroidRuntime(1512):     at com.amzngscrum.alpha2.SMSReceiver.onReceive(SMSReceiver.java:45)
03-05 11:47:47.430: E/AndroidRuntime(1512):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2229)
03-05 11:47:47.430: E/AndroidRuntime(1512):     ... 10 more

编辑2 这是MainActivity.class中的popuateview

public static void populateView(String sender, String message, String timestamp) {


    if(sender == null || message == null || timestamp == null){
    } else {
        final DataHolder data = new DataHolder();
        data.setSenderCPNumber(sender);
        data.setSenderMessage(message);
        data.setSenderTimestamp(timestamp);
        customListViewValues.add(data);
    }

    adapter = new CustomAdapterr( customListView, customListViewValues);
    lv.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    cnt.setText("On Queue: " + lv.getAdapter().getCount());
    start();
}

和CustomAdapterr.class的完整代码

public class CustomAdapterr extends BaseAdapter implements OnClickListener {

private Activity activity;
private ArrayList data = null;
private static LayoutInflater inflater = null;
DataHolder tempValues = null;
int i = 0;

public CustomAdapterr(Activity a, ArrayList d) {
     activity = a;
     data = d;
     inflater = ( LayoutInflater )activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    public TextView cp;
    public TextView mess;
    public TextView ts;
}

public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    ViewHolder holder;

    if(convertView == null){

        vi = inflater.inflate(R.layout.list_row, null);

        holder = new ViewHolder();
        holder.cp = (TextView) vi.findViewById(R.id.cpnumber);
        holder.mess = (TextView) vi.findViewById(R.id.messages);
        holder.ts = (TextView) vi.findViewById(R.id.timestamp);

        vi.setTag(holder);
    } else {
        holder = (ViewHolder) vi.getTag();
    }

    if(data.size()<=0) {
        //no data here
    }
    else {
        tempValues = null;
        tempValues = (DataHolder) data.get(position);

        holder.cp.setText(tempValues.getSenderCPNumber());
        holder.mess.setText(tempValues.getSenderMessage());
        holder.ts.setText(tempValues.getSenderTimestamp());
        vi.setOnClickListener(new OnItemClickListener(position));
    }
    return vi;
}

private class OnItemClickListener implements OnClickListener{

    private int mPosition;

    OnItemClickListener(int position){
         mPosition = position;
    }

    @Override
    public void onClick(View arg0) {
      MainActivity mainClick = (MainActivity) activity;
      mainClick.onItemClick(mPosition);
    }               
}

@Override
public void onClick(View v) {
}   

}

1 个答案:

答案 0 :(得分:0)

NullPointerException是在适配器的构造函数中生成的,我不知道你的活动中customListView是什么,但是它是null,你应该考虑检查它,最初解决问题的变化这样:

adapter = new CustomAdapterr(customListView, customListViewValues); 为此:

adapter = new CustomAdapterr(this, customListViewValues);

使用populateView方法。