2015-05-22 74 views
-4

我试图运行一个特定的方法时调用线程。像这样:为什么我的应用程序在Android中运行Runnable线程时崩溃?

private void changeSize(final Bitmap image) { 
    Thread task = new Thread(new Runnable() { 
    @Override 
    public void run() { 
      byte[] image; 
      int width = image.getWidth(); 
      int height = image.getHeight(); 
      int newHeight = 0, newWidth = 0; 
      if (width > 350 || height > 350) { 
       if (width > height) { 
        newHeight = 350; 
        newWidth = (newHeight * width)/height; 
       } else { 
        nyBredden = 350; 
        newHeight = (newWidth * height)/width; 
       } 
      } else { 
       Toast.makeText(context, "test", Toast.LENGTH_LONG).show(); 
       } 
      Bitmap sizeChanged = Bitmap.createScaledBitmap(image, newWidth, newHeight, true); 
      if (sizeChanged != null) { 
       Toast.makeText(context, "test", Toast.LENGTH_SHORT).show(); 
      } else { 
       Toast.makeText(context, "test", Toast.LENGTH_SHORT).show(); 
      } 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

      if (sizeChanged.getHeight() >= 350 || sizeChanged.getWidth() >= 350) { 
       sizeChanged.compress(Bitmap.CompressFormat.JPEG, 90, stream); 
      } else { 
       sizeChanged.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
      } 

      image = stream.toByteArray(); 
      if (image != null) { 
       myImage(image); //method 
       File path = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
       String namechanged = edMyType.getText().toString() + "_scalledDown" + ".jpg"; 

       File file = new File(path, namechanged); 
       FileOutputStream fos = null; 
       try { 
        fos = new FileOutputStream(file); 
        fos.write(image); 
        fos.flush(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } finally { 
        try { 
         if (fos != null) { 
          fos.close(); 
         } 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
      } else { 
       Toast.makeText(context, "test", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 
    }); 
    task.start(); 
} 

当我调用这个方法时,我的应用程序崩溃了。我试图调试,但我不明白的变量,例如:

task: "Thread[Thread-149,5,main]" 
hasBeenStarted = false 

当我按下按钮拍照时调用该方法。请任何人知道我做错了什么或者是否正确运行这样的线程?

+5

发布logcat输出! –

+0

什么是一些代码? –

+0

发布可运行内部的逻辑。 – Kartheek

回答

2

在后台线程不能显示Toast秒。

要么移除吐司,要么使用例如为了调试的目的而进行日志记录,或者通过使用例如脚本将祝词转移到主UI线程。 ActivityrunOnUiThread()Handler

+0

但上下文就是有代码的Activity。 – carl

+0

上下文无关紧要。敬酒只在UI线程上工作。 – laalto

+0

我尝试没有敬酒,我得到同样的崩溃! – carl

-2
Thread splashThread = new Thread() { 
      public void run() { 
       synchronized (this) { 
        try { 
         wait(2000); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        startActivity(new Intent(getApplicationContext(), 
          MainActivity.class)); 
        finish(); 
       } 
      } 
     }; 
     splashThread.start(); 

尝试使用我的代码。我希望这能帮到您。

0

希望这有助于!

final Handler handler = new Handler(); 

      handler.postDelayed(new Runnable() { 

       @Override 
       public void run() 

       { 
         //do your stuff here ,but you canot update ui from any other thread 
} 
      }, 1000); 
+0

我现在正在显示整个代码。 – carl

1

你应该使用处理器...

private void changeSize(final Bitmap image) { 
Thread task = new Thread(new Runnable() { 
    @Override 
    public void run() { 

    } 
     private final Handler handler = new Handler() { 
      //some code here.. 
     }; 
}); 
task.start(); 
} 
相关问题