Android:线程停止工作

时间:2013-05-10 20:02:45

标签: java android

它从频道读取图像并将其写入卡。卡片中的图像完全写好,很好。唯一的问题是挂起的线程,并且在该循环之后不执行任何行。

protected Bitmap doInBackground(String... arg0) {
    // TODO Auto-generated method stub'
      // Runtime.getRuntime().availableProcessors();
    System.out.println("Inside doinback"+ RemoteScreen.out.toString());
        try {
            RemoteScreen.out.write(210);
            //Home.threadloop = false;  
             Bitmap bitmap = null;  
            String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
            String fileName = "a.png";
            String imageInSD = baseDir + File.separator + fileName; 
                System.out.println(imageInSD);
            if (is!= null) {
                FileOutputStream fos = null;
                BufferedOutputStream bos = null;
                try {
                    // Log.i("IMSERVICE", "FILERECCC-1");
                    //ContextWrapper context = null;
                    fos = new FileOutputStream(imageInSD);   
                    bos = new BufferedOutputStream(fos);

                    byte[] aByte = new byte[1024]; 
                    int bytesRead;  
                    //thread stuck in this loop and does not move forward 
                    while ( (bytesRead = is.read(aByte)) > 0  ) {
                        if (bytesRead == 1)
                            break; 
                        bos.write(aByte, 0, bytesRead);  
                   System.out.println("Loop"+aByte); 

                    }   

                   bos.close();

                Log.i("IMSERVICE", "out of loop");     
                    java.io.FileInputStream in = new FileInputStream(imageInSD);
               bitmap = BitmapFactory.decodeStream(in);
                  bitmap = BitmapFactory.decodeFile(imageInSD); 

            Log.i("IMSERVICE", "saved");
                if (bitmap != null) 
                    System.out.println(bitmap.toString());

                } catch (IOException ex) {    
                    // Do exception handling      
             //     Log.i("IMSERVICE", "exception ");
                }
            }
             publishProgress(b);   
             b = null; 
             ch = 4646;

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



     return null;
}

日志跟踪显示以下错误。

   05-10 20:34:42.715: E/AndroidRuntime(1135): FATAL EXCEPTION: AsyncTask #3
  05-10 20:34:42.715: E/AndroidRuntime(1135): java.lang.RuntimeException: An error occured while executing doInBackground()
  05-10 20:34:42.715: E/AndroidRuntime(1135):   at android.os.AsyncTask$3.done(AsyncTask.java:299)
    05-10 20:34:42.715: E/AndroidRuntime(1135):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
   05-10 20:34:42.715: E/AndroidRuntime(1135):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
    05-10 20:34:42.715: E/AndroidRuntime(1135):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
    05-10 20:34:42.715: E/AndroidRuntime(1135):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  05-10 20:34:42.715: E/AndroidRuntime(1135):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
 05-10 20:34:42.715: E/AndroidRuntime(1135):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
 05-10 20:34:42.715: E/AndroidRuntime(1135):    at java.lang.Thread.run(Thread.java:856)
 05-10 20:34:42.715: E/AndroidRuntime(1135): Caused by: java.lang.NullPointerException
 05-10 20:34:42.715: E/AndroidRuntime(1135):    at com.rms.remotedesktop1.screencahnge.doInBackground(screencahnge.java:43)
 05-10 20:34:42.715: E/AndroidRuntime(1135):    at com.rms.remotedesktop1.screencahnge.doInBackground(screencahnge.java:1)
 05-10 20:34:42.715: E/AndroidRuntime(1135):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
 05-10 20:34:42.715: E/AndroidRuntime(1135):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)

2 个答案:

答案 0 :(得分:5)

根据logcat,代码在第43行崩溃时出现空指针异常,我无法判断哪一行是43。但是:

答案 1 :(得分:0)

我自己解决了这个问题。由于代码中的逻辑错误,静态变量中的错误正在改变其值。