当我检查互联网连接时,asynctask 出现运行时错误

发布于 2024-12-12 14:06:46 字数 2600 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是我以为 2024-12-19 14:06:47

您不能在 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.

简单 2024-12-19 14:06:47

content 为 null,因此您会得到 NullPointerException。您需要为 content 分配上下文,您可以分配活动本身,如下所示:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    content = this; // this is the important line
    // The rest goes here...
}

content is null, so you get NullPointerException. You need to assign a context to content, you can probably assign the activity itself, like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    content = this; // this is the important line
    // The rest goes here...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文