我在AsyncTask期间尝试显示和隐藏对话框时遇到了问题,我在Android应用中为活动做了整理。
以下是我目前正在做的事情:
public void syncCard(final Tag tag) {
syncTask = new SyncTask();
syncTask.execute(tag);
}
private class SyncTask extends AsyncTask<Tag, Void, Void> {
private final Dialog dialog;
private boolean mException;
public SyncTask() {
// Prepare dialog
dialog = buildSyncingDialog();
}
@Override
protected void onPreExecute() {
dialog.show();
}
@Override
protected Void doInBackground(Tag... params) {
mTagcomm = IsoDep.get(params[0]);
if (mTagcomm == null) {
//TODO - Handle communication error with the present card
}
mException = false;
try {
// Open connection
mTagcomm.connect();
lastAts = getAts(mTagcomm);
mProvider.setmTagCom(mTagcomm);
EmvParser parser = new EmvParser(mProvider, true);
mCard = parser.readEmvCard();
if (mCard != null) {
mCard.setAtrDescription(extractAtsDescription(lastAts));
}
} catch (IOException e) {
mException = true;
} finally {
// close tagcomm
IOUtils.closeQuietly(mTagcomm);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (dialog.isShowing()) {
dialog.dismiss();
}
if (!mException) {
if (mCard != null) {
showPaymentCardDetails();
} else if (mCard.isNfcLocked()) {
//TODO - Show error message informing user the card is locked
}
} else {
}
}
private Dialog buildSyncingDialog() {
String syncingText = String.format( "%s\n%s",
getString( R.string.syncing ), getString( R.string.do_not_remove_card ) );
Dialog dialog = new CustomDialog( PaymentCardRegisterActivity.this,
syncingText, R.layout.layout_syncing );
dialog.setCancelable( false );
return dialog;
}
@Override
public void onCancelled() {
dialog.dismiss();
}
}
我看到以下错误:
java.lang.RuntimeException:无法在线程内创建处理程序 没有调用Looper.prepare()
我遇到了这个旧答案 - Can't create handler inside thread which has not called Looper.prepare()
但我仍然对我需要做的事情感到困惑,该帖子的最后一个回复是关于从UI线程运行任务,我将如何使用上面的代码执行此操作?< / p>
修改
我的活动课程扩展NfcActivity
,因为我正在使用此课程阅读非接触式卡片详情。
我的syncCard
正在从另一个后台线程(我相信)中调用,如此
@Override
public void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
processIntent(intent);
}
private void processIntent(final Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Tag tagFromIntent = intent.getParcelableExtra( NfcAdapter.EXTRA_TAG );
onTagDiscovered(tagFromIntent);
}
}
@Override
public void onTagDiscovered( final Tag tag ) {
super.onTagDiscovered(tag);
try {
PaymentCardRegisterActivity.this.syncCard(tag);
} catch ( Exception e) {
Log.e( PaymentCardRegisterActivity.class.getName(), "Failed to sync card", e );
// Return to start activity
finish();
}
}
答案 0 :(得分:1)
如果onTagDiscovered
在不是UI线程的线程上运行,那么您应该将对syncCard
的调用分派给主线程。如果您引用Activity
,则可以使用runOnUiThread
并发布Runnable
。这将确保在主线程上执行Runnable
。
在您的代码中,我认为您需要更改此
@Override
public void onTagDiscovered( final Tag tag ) {
super.onTagDiscovered(tag);
try
{
PaymentCardRegisterActivity.this.syncCard(tag);
}
catch ( Exception e)
{
Log.e( PaymentCardRegisterActivity.class.getName(), "Failed to sync card", e );
// Return to start activity
finish();
}
}
进入这个
@Override
public void onTagDiscovered( final Tag tag ) {
super.onTagDiscovered(tag);
runOnUiThread(new Runnable() {
public void run() {
try {
PaymentCardRegisterActivity.this.syncCard(tag);
} catch ( Exception e) {
Log.e( PaymentCardRegisterActivity.class.getName(), "Failed to sync card", e );
}
}
});
}