Android runOnUiThread未在AsyncTask中运行
我正在编写一个程序,从服务器获取MySql数据(使用JSON),并相应地更新UI 我使用AsyncTask从服务器获取两种类型的数据Android runOnUiThread未在AsyncTask中运行,android,multithreading,Android,Multithreading,我正在编写一个程序,从服务器获取MySql数据(使用JSON),并相应地更新UI 我使用AsyncTask从服务器获取两种类型的数据 1) Bubble Answers 2) Comments parseBubbleAnswers方法成功运行并更新UI, 但是parseComments类是AsyncTask,在doInBackground中调用parseComments方法,它没有运行runOnUiThread(new Runnable(){run()}) 有人能帮我解决这个问题吗 这是我的密
1) Bubble Answers
2) Comments
parseBubbleAnswers方法成功运行并更新UI,
但是parseComments类是AsyncTask,在doInBackground中调用parseComments方法,它没有运行runOnUiThread(new Runnable(){run()})代码>
有人能帮我解决这个问题吗
这是我的密码:
public class FetchServer extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
String photoId = "1"; // photo id for which the data is fetched
checkBubbleData(photoId); // which call AsyncTask - 2 differnt calls
}
public void checkBubbleData(String photoId)
{
new parseBubbleAnswers().execute(photoId); // to fetch bubble answers
new parseComments().execute(photoId); // to fetch comments
}
class parseBubbleAnswers extends AsyncTask<String, Integer,String>
{
@Override
protected String doInBackground(String... params)
{
// TODO Auto-generated method stub
Looper.prepare();
parseBubbleAnswers(); // which has runOnUiThread(new Runnable() which updates (successfully !) the UI
return null;
}
}
class parseComments extends AsyncTask<String, Integer,String>
{
@Override
protected String doInBackground(String... params)
{
// TODO Auto-generated method stub
Looper.prepare();
String parseComReturn = parseComments();
if(parseComReturn=="end")
{
commentBuilder(); // which will update UI after fetch data by parseComments() method
}
}
}
public void commentBuilder()
{
runOnUiThread(new Runnable() // while debugging, it comes here, on Step Over it stick for 2 times and then move at the end of method without error
{
public void run()
{
// update UI code
}
});
}
}
公共类FetchServer扩展活动
{
创建时受保护的void(Bundle savedInstanceState)
{
String photoId=“1”;//为其获取数据的照片id
checkBubbleData(photoId);//哪个调用AsyncTask-2个不同的调用
}
公共无效检查BubbleData(字符串ID)
{
新建parseBubbleAnswers().execute(photoId);//获取气泡答案
new parseComments().execute(photoId);//获取注释
}
类parseBubbleAnswers扩展异步任务
{
@凌驾
受保护的字符串doInBackground(字符串…参数)
{
//TODO自动生成的方法存根
Looper.prepare();
parseBubbleAnswers();//其中包含runOnUiThread(new Runnable(),它更新(成功!)UI
返回null;
}
}
类parseComments扩展异步任务
{
@凌驾
受保护的字符串doInBackground(字符串…参数)
{
//TODO自动生成的方法存根
Looper.prepare();
字符串parseComReturn=parseComments();
if(parseComReturn==“end”)
{
commentBuilder();//它将在通过parseComments()方法获取数据后更新UI
}
}
}
公共建筑商()
{
runOnUiThread(new Runnable()//调试时,它会出现在这里,在步骤上停留2次,然后在方法末尾移动,没有错误
{
公开募捐
{
//更新用户界面代码
}
});
}
}
runOnUiThread
是活动的一种方法
,异步任务
与活动无关
然而,AsyncTask已经在UI线程上运行了,并且正是为了实现这一点而设计的
只需处理PostExecute中的UI更改
尝试以下方法:
首先创建一个处理程序
:
Handler mHandler = new Handler();
更改此项,
public void commentBuilder()
{
runOnUiThread(new Runnable() // while debugging, it comes here, on Step Over it stick for 2 times and then move at the end of method without error
{
public void run()
{
// update UI code
}
});
}
public void commentBuilder()
{
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (isRunning) {
try {
// Thread.sleep(10000);
mHandler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// Write your code here to update the UI.
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();
}
与,
public void commentBuilder()
{
runOnUiThread(new Runnable() // while debugging, it comes here, on Step Over it stick for 2 times and then move at the end of method without error
{
public void run()
{
// update UI code
}
});
}
public void commentBuilder()
{
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (isRunning) {
try {
// Thread.sleep(10000);
mHandler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// Write your code here to update the UI.
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();
}
完成UI后,停止此线程
isRunning = false;
编辑:
尝试以以下方式使用异步任务:
class parseComments extends AsyncTask<String, Integer,String>
{
protected String doInBackground(String... params) {
String parseComReturn = parseComments();
return parseComReturn;
}
protected void onPostExecute(String result) {
if(result.equals("end"))
{
commentBuilder();
}
}
}
类parseComments扩展异步任务
{
受保护的字符串doInBackground(字符串…参数){
字符串parseComReturn=parseComments();
回归与回归;
}
受保护的void onPostExecute(字符串结果){
如果(结果等于(“结束”))
{
注释生成器();
}
}
}
谢谢。我也面临类似的问题 只需将Activity类的引用传递给parseComments类
class parseComments extends AsyncTask<String, Integer,String>{
Activity activity;
public parseComments(Activity activity){
this.activity = activity;
}
}
它只适用于活动类,而不适用于上下文类。受保护的void onPostExecute(字符串结果){}方法未执行您需要返回它在
doInBackground
完成后执行的,尝试重写它并记录日志以查看它是否被调用检查我在下面发布的解决方案。让我知道是否仍然存在此问题。谢谢。我使用了此方法,当调试器到达})时,我也在调试代码。start();这一行,它在方法的末尾没有任何错误,它假设进入run()内部,但没有执行run(),为什么?@SunilLohar您声明了boolean isRunning=true代码>在应用程序顶部?因为它对我很有效。不,它对我不起作用,我已经声明布尔isRunning=true;但主要问题是,它并没有进入run()内部,在start()之后,它会在调用该方法的AsyncTask中执行return语句。它是说在调试器线程[AsycntTask#2](挂起)@SunilLohar中,请查看我发布的编辑过的答案,对于测试,只需停止parseBubbleAnswers-taks
,并使用这些更改运行此任务。