设想:
我正在使用以下代码从片段发送广播
Intent intent=new Intent();
intent.setAction("writeBalanceToNFCCard");
intent.putExtra("balance", Double.valueOf(result));
getActivity().sendBroadcast(intent);
在 MainActivity 中,我收到了
private final BroadcastReceiver mReceiverWriteNFC=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
double newBalance = intent.getDoubleExtra("balance", 0);
write(newBalance);
}
};
在 MainActivity 的 onCreate() 中,我正在注册
IntentFilter writeNFCFilter=new IntentFilter("writeBalanceToNFCCard");
registerReceiver(mReceiverWriteNFC, writeNFCFilter);
问题:它有时会工作....有时我会收到以下错误
我究竟做错了什么???
编辑:这是写方法
private void write(double newBalance) {
Log.d(TAG, "Write to tag");
//to display new balance on homefragment.
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("newbalance",String.valueOf(newBalance));
editor.commit();
paymentData=new MyPaymentData();
paymentData.setUid(operations.getUid());
paymentData.setCurrency("dkk");
paymentData.setAmount(newBalance);
Log.d(TAG,"WRITE NFC: BALANCE="+ String.valueOf(newBalance));
byte[] bytes = encodePayload(paymentData);
byte[] encrypted = encrypt(bytes);
new WriteTask().execute(encrypted,bytes);
}
编辑2:
出现错误后,见下图,write() 方法实际上被调用并执行其功能。问题是客户绝对不喜欢看到丑陋的应用程序崩溃消息..!!