java.lang.RuntimeException: 将消息发送到死的线程上处理程序的处理程序 (android.os.Handler)
2017-10-25 10:08
579 查看
在我的应用程序中我使用 IntentService 的发送短信。
@Override protected void onHandleIntent(Intent intent) { Bundle data = intent.getExtras(); String[] recipients = null; String message = getString(R.string.unknown_event); String name = getString(R.string.app_name); if (data != null && data.containsKey(Constants.Services.RECIPIENTS)) { recipients = data.getStringArray(Constants.Services.RECIPIENTS); name = data.getString(Constants.Services.NAME); message = data.getString(Constants.Services.MESSAGE); for (int i = 0; i < recipients.length; i++) { if(!StringUtils.isNullOrEmpty(recipients[i])) { try { Intent sendIntent = new Intent(this, SMSReceiver.class); sendIntent.setAction(Constants.SMS.SEND_ACTION); PendingIntent sendPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT); Intent deliveryIntent = new Intent(this, SMSReceiver.class); deliveryIntent.setAction(Constants.SMS.DELIVERED_ACTION); PendingIntent deliveryPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, deliveryIntent, PendingIntent.FLAG_UPDATE_CURRENT); SmsManager.getDefault().sendTextMessage(recipients[i].trim(), null, "[" + name + "] " + message, sendPendingIntent, deliveryPendingIntent); } catch (Exception e) { Log.e(TAG, "sendTextMessage", e); e.printStackTrace(); Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); MainActivity.instance.writeToLogFile(e.getMessage(), System.currentTimeMillis()); } } } } }
ecTransact(Binder.java:351) W/MessageQueue(7180): at dalvik.system.NativeStart.run(Native Method)我的 SMSReceiver 位于另一个类。我怎样才能解决这个问题?
解决方法 1:
这里的问题是创建的Toast里面都由一个线程
IntentService。该系统将使用
Handler与此线程显示和隐藏关联
Toast。第一次
Toast将显示正确,但当系统试图隐藏它之后,
onHandleIntent方法完成,因为将引发"将消息发送到死的线程上处理程序"的错误中至极的线程
Toast创建的不再是有效的和
Toast不会消失。若要避免此应显示
Toast发布一条消息到主线程。下面是一个示例:
// create a handler to post messages to the main thread Handler mHandler = new Handler(getMainLooper()); mHandler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show(); } });
相关文章推荐
- java.lang.RuntimeException: Handler (android.os.Handler) sending message to a Handler on a dead thre
- 【异常处理】java.lang.ClassNotFoundException: Didn't find class "android.os.PersistableBundle"
- Android java.lang.RuntimeException: Can\'t create handler inside thread that has not called Looper.prepare()
- 关于Android程序的java.lang.RuntimeException: Unable to instantiate activity ComponentInfo错误
- Android—子线程更新UI问题( java.lang.RuntimeException: Can't create handler inside thread that has not cal)
- Android java.lang.RuntimeException: Can't create handler inside thread that has not called
- java.lang.RuntimeException: Unable to start activity ComponentInfo....android.os.NetworkOnMainThread
- 关于Android程序的java.lang.RuntimeException: Unable to instantiate activity ComponentInfo错误
- 线程 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepa
- android java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pr
- Android 系列 4.11使用活动线程队列和处理程序在线程之间发送消息
- java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 520536 bytes
- Android非UI主线程中,若干普通Java线程使用Handler发送接收消息
- Android 异常 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper
- Android学习—— Handler 的消息发送,在新的线程处理消息
- android 程序错误处理全局处理 UncaughtExceptionHandler
- Android最新的开发环境运行原来环境开发的程序报java.lang.ClassNotFoundException错误解决
- ERROR/AndroidRuntime: Caused by: java.lang.ClassCastException: android.widget.button
- android 程序错误处理全局处理 UncaughtExceptionHandler
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()