2016-03-05 95 views
0

我寫了一個可以接收短信的android應用程序。該應用程序正在工作。它可以接收短信,但每次安裝時都是新鮮的。每次我從模擬器發送短信到應用程序(無需手動打開)它崩潰或強制關閉,但如果我手動打開它可以接收短信沒有問題自定義短信接收器首次收到短信崩潰[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 

EDIT 2 這裏是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(); 
} 

和CustomAdapterr.class

的全碼popuateview
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) { 
} 

}

+0

你可以提供堆棧跟蹤,放置嗎? –

+0

好吧,先生,我會重新啓動我的模擬器 – something

+0

@EuryPérezBeltré我包括堆棧跟蹤 – something

回答

0

NullPointerException在適配器的構造產生的,我不知道什麼是你的活動customListView但它是空,你應該考慮檢查出來,初步解決問題更改此:

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

adapter = new CustomAdapterr(this, customListViewValues);

在你populateView方法。

+0

後,將其更改爲「這個」它有錯誤 「不能在靜態上下文中使用此」 – something

+0

好吧,我看,事情是不是一個很好的做法,有一個活動中的靜態方法,它甚至更糟地稱它們從外面(例如服務或接收器)。要與您的活動溝通,請考慮使用LocalBroadcastManager。檢查這個例子:https://gist.github.com/Antarix/8131277。這也是有用的:http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager 這是更像Android的方式去。 –

+0

您應該從您的'populateViews'方法中移除靜態標記,並使用LocalBroadcastManager與您的活動進行通信。 –