当我检查互联网连接时,asynctask 出现运行时错误
我在 doInBackground
上遇到 asynctask
问题,因为我收到此代码专门针对的运行时错误:
ConnectivityManager connectivity = (ConnectivityManager) content.getSystemService(Context.CONNECTIVITY_SERVICE);
((我想在 inamgeview
中查看图像> 但首先检查连接是否处于活动状态,这些操作是在不扩展活动的单独类中执行的)):
这是主要活动:
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);
}
}
}
我已经更正了错误,但我现在在调试视图中看到了这一点:
Thread [<17> AsyncTask #1] (Stepping)
ThreadPoolExecutor$Worker.run() line: 672
Thread.run() line: 1060
并且不显示imageview
上的任何内容然后如果我触摸屏幕返回:
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]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在 doInBackground() 中执行任何 gui 操作,并且您正在调用显示 Toast 消息的 funciton。查看有关 AsyncTask 的文档。
You cannot do any gui stuff in
doInBackground()
, and you are calling funciton which shows Toast message. Check documentation on AsyncTask.content
为 null,因此您会得到 NullPointerException。您需要为content
分配上下文,您可以分配活动本身,如下所示:content
is null, so you get NullPointerException. You need to assign a context tocontent
, you can probably assign the activity itself, like this: