开发者

Android Threading

Hey, So im newish to android and Im lost because although my code uses AsyncTask for it's heavy lifting I am still getting a ANR error when I run my one class. So here is the relevant peices of my code:

package com.cody.color;

import android.app.Activity;
import android.os.Bundle;


public class Play extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);     
    System.out.println("about to start threader");
    new GuiThreader().execute();
    System.out.println("Threader finished");
    setContentView(R.layout.colorboard_small);
}
}



package com.cody.color;

import android.os.AsyncTask;

public class GuiThreader extends AsyncTask<Void, Void, Void>{

@Override
prote开发者_JAVA技巧cted Void doInBackground(Void... params) {
    // TODO Auto-generated method stub
    System.out.println("threader moving");
    GUIdriver game = new GUIdriver();
    game.play();
    return null;
}

}


There aren't any problems with the code above that would cause an ANR, so most likely one of the following is happening:

  • You are trying to modify the user interface (UI) from doInBackground
  • An exception is occuring in your doInBackground

If you post the LogCat output or stacktrace when you get the error then it will be easier to identify.


welcome to the world of JAVA programming :) When you encounter an error your best tool to solve it is usually going to be a stacktrace. You need to learn how to analyze a stacktrace and also to include it in your questions...

What is a stacktrace you ask? The simplistic answer is that a stacktrace is an error message (although it contains a lot more info than only that). In case of an exception it tells the programmer WHAT happened, WHERE it happened and WHEN it happened. If you want to program in JAVA you need to get yourself quite familiar with them. I recommend you read some JAVA tutorials before diving into Android development.

As for your code, without the stacktrace I cannot say more... there is no obvious error except the fact that game.play(); looks like it might contain some changes to the UI, which are not allowed in another thread than the main thread (that's why AsyncTask has onPostExecute());

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜