无法在 AsyncTask 内未调用 Looper.prepare() 的线程内创建处理程序
因为我的应用程序很大程度上依赖于网络,所以我希望我的应用程序不应该挂起。 所以我尝试使用 Asyncask
我有一个
- MainActivity 扩展了 Second Activity
- 中的内部类
- AsyncTask 作为 MainActivity Second Activity
Main Activity 和 Async Task :
public class MainActivity extends secondActivity implements OnClickListener{
@Override
public void onClick(View arg0) {
new triggerAsync().execute(str);
}
private class triggerAsync extends AsyncTask<String,String,String>
{
@Override
protected String doInBackground(String... str) {
try {
setConnection();
getID(txtUserName);
Log.d("Inside Background","string here"+str[0]);
// postOnWall(str[0]);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}}}
Second Activity :
public class SecondActivity extends Activity {
public void setConnection() {
//My stuffs
}
public void getID(TextView txtUserName) {
// here i'm calling another function inside package
if (isSession()) {
Log.d(TAG, "sessionValid");
mAsyncRunner.request("me", new IDRequestListener());
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMS, new LoginDialogListener());
}
}
/////////////// Code continues.....}
这有什么错误代码,请帮我解决这个问题
我遇到以下错误
08-15 11:01:07.211: WARN/System.err(462): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-15 11:01:07.251: WARN/System.err(462): at android.os.Handler.<init>(Handler.java:121)
08-15 11:01:07.251: WARN/System.err(462): at android.app.Dialog.<init>(Dialog.java:101)
08-15 11:01:07.271: WARN/System.err(462): at android.app.Dialog.<init>(Dialog.java:123)
08-15 11:01:07.271: WARN/System.err(462): at com.facebook.android.FbDialog.<init>(FbDialog.java:63)
08-15 11:01:07.281: WARN/System.err(462): at com.facebook.android.Facebook.dialog(Facebook.java:622)
08-15 11:01:07.291: WARN/System.err(462): at com.facebook.android.Facebook.startDialogAuth(Facebook.java:297)
08-15 11:01:07.301: WARN/System.err(462): at com.facebook.android.Facebook.authorize(Facebook.java:195)
08-15 11:01:07.311: WARN/System.err(462): at com.facebook.android.Facebook.authorize(Facebook.java:114)
08-15 11:01:07.321: WARN/System.err(462): at com.singleupdater.FacebookActivity.getID(FacebookActivity.java:61)
08-15 11:01:07.331: WARN/System.err(462): at com.singleupdater.SingleStausUpdater$postToTwitter.doInBackground(SingleStausUpdater.java:149)
08-15 11:01:07.341: WARN/System.err(462): at com.singleupdater.SingleStausUpdater$postToTwitter.doInBackground(SingleStausUpdater.java:1)
08-15 11:01:07.351: WARN/System.err(462): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-15 11:01:07.361: WARN/System.err(462): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-15 11:01:07.371: WARN/System.err(462): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-15 11:01:07.371: WARN/System.err(462): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-15 11:01:07.391: WARN/System.err(462): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-15 11:01:07.391: WARN/System.err(462): at java.lang.Thread.run(Thread.java:1096)
Because my app depends much on Network, I wish my app should not hang up.
So i tried to use Asyncask
I have a
- MainActvity extends Second Activity
- AsyncTask as Inner Class in MainActivity
- Second Activity
Main Activity and Async Task :
public class MainActivity extends secondActivity implements OnClickListener{
@Override
public void onClick(View arg0) {
new triggerAsync().execute(str);
}
private class triggerAsync extends AsyncTask<String,String,String>
{
@Override
protected String doInBackground(String... str) {
try {
setConnection();
getID(txtUserName);
Log.d("Inside Background","string here"+str[0]);
// postOnWall(str[0]);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}}}
Second Activity :
public class SecondActivity extends Activity {
public void setConnection() {
//My stuffs
}
public void getID(TextView txtUserName) {
// here i'm calling another function inside package
if (isSession()) {
Log.d(TAG, "sessionValid");
mAsyncRunner.request("me", new IDRequestListener());
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMS, new LoginDialogListener());
}
}
/////////////// Code continues.....}
What is the error in this code, please help me to solve this
I'm getting below error
08-15 11:01:07.211: WARN/System.err(462): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-15 11:01:07.251: WARN/System.err(462): at android.os.Handler.<init>(Handler.java:121)
08-15 11:01:07.251: WARN/System.err(462): at android.app.Dialog.<init>(Dialog.java:101)
08-15 11:01:07.271: WARN/System.err(462): at android.app.Dialog.<init>(Dialog.java:123)
08-15 11:01:07.271: WARN/System.err(462): at com.facebook.android.FbDialog.<init>(FbDialog.java:63)
08-15 11:01:07.281: WARN/System.err(462): at com.facebook.android.Facebook.dialog(Facebook.java:622)
08-15 11:01:07.291: WARN/System.err(462): at com.facebook.android.Facebook.startDialogAuth(Facebook.java:297)
08-15 11:01:07.301: WARN/System.err(462): at com.facebook.android.Facebook.authorize(Facebook.java:195)
08-15 11:01:07.311: WARN/System.err(462): at com.facebook.android.Facebook.authorize(Facebook.java:114)
08-15 11:01:07.321: WARN/System.err(462): at com.singleupdater.FacebookActivity.getID(FacebookActivity.java:61)
08-15 11:01:07.331: WARN/System.err(462): at com.singleupdater.SingleStausUpdater$postToTwitter.doInBackground(SingleStausUpdater.java:149)
08-15 11:01:07.341: WARN/System.err(462): at com.singleupdater.SingleStausUpdater$postToTwitter.doInBackground(SingleStausUpdater.java:1)
08-15 11:01:07.351: WARN/System.err(462): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-15 11:01:07.361: WARN/System.err(462): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-15 11:01:07.371: WARN/System.err(462): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-15 11:01:07.371: WARN/System.err(462): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-15 11:01:07.391: WARN/System.err(462): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-15 11:01:07.391: WARN/System.err(462): at java.lang.Thread.run(Thread.java:1096)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您的某些方法 -
setConnection()
或getID(txtUserName)
尝试从后台线程访问 UI 并且失败。将您的 UI 逻辑移至onPostExecute
以在 UI 线程上进行处理。It seems that some of your methods -
setConnection()
orgetID(txtUserName)
tries to access UI from background thread and fails. Move your UI logic toonPostExecute
to be processed on UI thread.