0

I written an android application that can receive sms. The app is working. It can receive sms, but every time the app is fresh from install. Everytime I send sms from emulator to the app (without opening it manually) it crashes or force close but if I open it manually it can receive sms without issue

here is my code for if the app receive sms it will do this code

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);
        }

       }

}

Please someone help me with this. I want to receive sms without opening it manually.

EDIT Here is the stack trace

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

EDIT 2 here is the popuateview in MainActivity.class

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();
}

and full code of 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) {
}   

}

something
  • 99
  • 12

1 Answers1

0

The NullPointerException is produced in the constructor of the adapter, I don't know what is customListView in your activity but it's null and you should consider checking that out, initially to resolve the issue change this:

adapter = new CustomAdapterr(customListView, customListViewValues); for this:

adapter = new CustomAdapterr(this, customListViewValues);

In your populateView method.

Eury Pérez Beltré
  • 2,017
  • 20
  • 28
  • after changing it to "this" it has error "cannot use this on static context" – something Mar 05 '16 at 04:35
  • Ok I see, the thing is that is not a good practice to have static methods in an Activity and its even worse to call them from outside (a service or receiver for example as your case). To communicate with your activity consider using the LocalBroadcastManager. Check this example: https://gist.github.com/Antarix/8131277. This one is useful too: http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager This is the more Android-like way to go. – Eury Pérez Beltré Mar 05 '16 at 04:41
  • You should remove the static flag from your `populateViews` method and use the LocalBroadcastManager to communicate with your activity. – Eury Pérez Beltré Mar 05 '16 at 04:43
  • ok sir. i'll try it. thankyou – something Mar 05 '16 at 04:43
  • If the info is helpful for you, please accept the answer and I will update it accordingly :) – Eury Pérez Beltré Mar 05 '16 at 04:44