0

asynctask我对on有问题,doInBackground因为我在以下代码中遇到运行时错误:

ConnectivityManager connectivity = (ConnectivityManager) content.getSystemService(Context.CONNECTIVITY_SERVICE);

(((我想在 a 中查看图像,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]
4

2 回答 2

1

content为空,所以你得到 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...
}
于 2011-10-28T14:38:30.540 回答
1

您不能在 中执行任何 guidoInBackground()操作,并且您正在调用显示 Toast 消息的功能。检查AsyncTask 的文档

于 2011-10-28T14:40:26.657 回答