I have problem with asynctask on doInBackground because I get a runtime error this code specifically on:
ConnectivityManager connectivity = (ConnectivityManager) content.getSystemService(Context.CONNECTIVITY_SERVICE);
((I would like to see an image in a inamgeview but first check if the connection is active these operations are performed in a separate class that does not extend activity)) :
this is the main activity:
public class TelevideoInternationalActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
String loc = "Nazionale";
static String home_page = "http://www.televideo.rai.it/televideo/pub/tt4web/Nazionale/16_9_page-100.png";
WorkAsyncIta wai = new WorkAsyncIta();
static String but = "";
Context content;
/************DECLARES**************/
RelativeLayout relativeLayout1;
ImageView imgpag;
ProgressBar probar;
/**********INITIALIZES**************/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgpag = (ImageView)findViewById (R.id.imgpag);
relativeLayout1 = (RelativeLayout) findViewById(R.id.RelativeLayout1);
probar = (ProgressBar) findViewById (R.id.probar);
wai.execute("http://www.televideo.rai.it/televideo/pub/tt4web/Nazionale/16_9_page-100.png" );
}
private class WorkAsyncIta extends AsyncTask<String, Void, Bitmap>
{
@Override
protected void onPreExecute()
{
probar.setVisibility(0);
}
@Override
protected Bitmap doInBackground(String... url) {
return ItaliaTelevideo.setHomePage(url[0], content);
}
@Override
protected void onPostExecute(Bitmap result) {
probar.setVisibility(1);
imgpag.setImageBitmap(result);
imgpag.setScaleType(ScaleType.FIT_XY);
}
}
}
I have corrected the error, but I see this in debug view now:
Thread [<17> AsyncTask #1] (Stepping)
ThreadPoolExecutor$Worker.run() line: 672
Thread.run() line: 1060
and do not display anything on imageview then if I touch the screen returns:
Thread [<3> main] (Suspended (exception IllegalStateException))
ViewRoot.handleMessage (Message) line: 1704
ViewRoot (Handler). DispatchMessage (Message) line: 99
Looper.loop () line: 123
ActivityThread.main (String []) line: 4203
Method.invokeNative (Object, Object [], Class, Class [], Class, int, boolean) line: not available [native method]
Method.invoke (Object, Object ...) line: 521
ZygoteInit MethodAndArgsCaller.run $ () line: 791
ZygoteInit.main (String []) line: 549
NativeStart.main (String []) line: not available [native method]
You cannot do any gui stuff in
doInBackground(), and you are calling funciton which shows Toast message. Check documentation on AsyncTask.