我寫了一個可以接收短信的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
的全碼popuateviewpublic 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
@EuryPérezBeltré我包括堆棧跟蹤 – something