来自线程的 Android 对话框
在以 AsyncTask 启动的后台线程中,我请求 root 访问权限。如果它被拒绝(su 未找到),我想告诉用户。我知道您不能直接从后台线程发布到 UI 线程,但是当我测试它时,通过对主活动的引用来执行此操作似乎效果很好。不过,现在它已发布到市场上,报告了这些错误:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:266)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
at java.lang.Thread.run(Thread.java:1020)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.app.Dialog.<init>(Dialog.java:100)
at android.app.AlertDialog.<init>(AlertDialog.java:96)
at android.app.AlertDialog$Builder.create(AlertDialog.java:891)
at com.sajarvis.albert.AlbertActivity.onCreateDialog(AlbertActivity.java:357)
at android.app.Activity.onCreateDialog(Activity.java:2747)
at android.app.Activity.createDialog(Activity.java:948)
at android.app.Activity.showDialog(Activity.java:2825)
at android.app.Activity.showDialog(Activity.java:2789)
at com.sajarvis.albert.AlbertActivity$HiyaTask.doInBackground(AlbertActivity.java:507)
at com.sajarvis.albert.AlbertActivity$HiyaTask.doInBackground(AlbertActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:252)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 4 more
这是对话框创建器中的代码(第 349-360 行)
case 1: //su denied
Log.w(TAG,"Su denied failure");
builder
.setTitle(R.string.su_denied_title)
.setMessage(R.string.su_denied_message)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
break;
以及后台线程中的第 501-511 行,我在其中调用对话框失败。
//Get ROOT access.
try {
process = Runtime.getRuntime().exec("su");
} catch (IOException e1) {
//TODO crashes here
activity.showDialog(1);
FlurryAgent.onError("4","su denied","Fail");
return success; //False, fail.
}
是activity.showDialog(1);是什么导致了崩溃?如果是这样,为什么它不会持续崩溃。如果不是,知道是什么吗?非常感谢。
In a background thread started with AsyncTask I request root access. If it's denied (su not found), I want to tell the user. I know you can't directly post to the UI thread from a background thread, but doing so through a reference to the main activity seemed to work well when I tested it. Now that it's published to the market, though, these errors are reported:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:266)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
at java.lang.Thread.run(Thread.java:1020)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.app.Dialog.<init>(Dialog.java:100)
at android.app.AlertDialog.<init>(AlertDialog.java:96)
at android.app.AlertDialog$Builder.create(AlertDialog.java:891)
at com.sajarvis.albert.AlbertActivity.onCreateDialog(AlbertActivity.java:357)
at android.app.Activity.onCreateDialog(Activity.java:2747)
at android.app.Activity.createDialog(Activity.java:948)
at android.app.Activity.showDialog(Activity.java:2825)
at android.app.Activity.showDialog(Activity.java:2789)
at com.sajarvis.albert.AlbertActivity$HiyaTask.doInBackground(AlbertActivity.java:507)
at com.sajarvis.albert.AlbertActivity$HiyaTask.doInBackground(AlbertActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:252)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 4 more
Here's the code in the dialog creator (lines 349-360)
case 1: //su denied
Log.w(TAG,"Su denied failure");
builder
.setTitle(R.string.su_denied_title)
.setMessage(R.string.su_denied_message)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
break;
And lines 501-511 in the background thread, where I call the dialog on fail.
//Get ROOT access.
try {
process = Runtime.getRuntime().exec("su");
} catch (IOException e1) {
//TODO crashes here
activity.showDialog(1);
FlurryAgent.onError("4","su denied","Fail");
return success; //False, fail.
}
Is activity.showDialog(1); what's causing the crash? If so, why does it not crash consistently. If it isn't, any idea what is? Thanks much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码仍然尝试从后台线程显示对话框。
在您的特定情况下,您想要做的是从
doInBackground()
返回结果,如果结果表明找不到su
,则在中显示对话框>onPostExecute()
。Your code still attempts to show dialog from background thread.
In you particular case, what you want to do is to return a result from
doInBackground()
and if result indicates thatsu
can't be found, show dialog inonPostExecute()
.