2013-02-17 62 views
1

我試圖在程序中顯示進度對話框,當按下刷新按鈕但應用程序關閉未解決的錯誤在調試器中創建的線程層次只能觸摸它。線程無法工作。應用程序關閉未解決。

public class MainActivity extends Activity { 
    Handler handler = new Handler(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     this.refreshView(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    public void refreshView(){ 
     ImageView img = (ImageView) findViewById(R.id.imageView1); 
     Bitmap bm = null; 
     InputStream is = null; 
     BufferedInputStream bis = null; 
     try 
     { 
      URLConnection conn = new URL("http://technomoot.edu.pk/$Common/Image/Content/ciit_logo.jpg").openConnection(); 
      conn.connect(); 
      is = conn.getInputStream(); 
      bis = new BufferedInputStream(is, 8192); 
      bm = BitmapFactory.decodeStream(bis); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     finally { 
      if (bis != null) 
      { 
       try 
       { 
        bis.close(); 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
       } 
      } 
      if (is != null) 
      { 
       try 
       { 
        is.close(); 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     } 
     img.setImageBitmap(bm); 
} 

    public void onRefresh(View view) { 

     final ProgressDialog dialog = ProgressDialog.show(this,"Loading","Loading the image of the Day"); 
     Thread th = new Thread(){ 
      public void run(){ 
       refreshView(); 
       handler.post(new Runnable(){ 
        public void run(){ 
         dialog.dismiss(); 
        } 
       } 
         ); 
      } 

     };th.start(); 
    } 

} 
+0

圖片URL只是temorary。來自url的原始圖片加載良好,並且工作正常,但是在pressign刷新時顯示等待或處理屏幕,但app在幾秒鐘後死亡。 – user2071857 2013-02-17 20:23:07

回答

1

你不能UI從工人Thread操縱!它是被禁止!如果您想更新UI只使用

  • runOnUiThread()
  • AsyncTask

因此,嘗試這樣的:

runOnUiThread(new Runnable() { 

    @Override 
    public void run() { 
     // do your work. 

    } 
}); 

AsyncTask比較複雜,也類屬提供som的類型喜歡類型的控制等。e好處你可以看一下這裏的例子:

+0

你的意思是用runOnUIThread代替handler.post ??????????? – user2071857 2013-02-17 20:43:43

+0

你實際上在那裏創建新的'Thread'只需放置'runOnUiThread()'方法。 – Sajmon 2013-02-17 20:53:38

1

要調用從一個非UI線程的UI操作,您可以使用runOnUiThread ..
或更好地利用AsynchTask

public YourTask extends AsyncTask<Object,Object,Object>{ //change Object to required type 
    ProgressDialog dialog; 
    Context context; 

public YourTask(Context context){ 
    this.context=context; 
} 

protected void onPreExecute(Object o){ 
    dialog = ProgressDialog.show(this,"Loading","Loading the image of the Day"); 
} 

protected void doInBackground(Object o){ 
    refreshView(); 
} 

protected void onPostExecute(Object o){ 
    img.setImageBitmap(bm); 
    dialog.dismiss(); 
} 
} 
+0

我是新的,不知道如何做到這一點。我發現這一點,並使用處理程序,我不知道如何使用。 andi仍然不能理解非ui線程的含義。任何人都可以用一種非常簡單的方式修復這些代碼,這樣我就能理解它。 – user2071857 2013-02-17 20:25:08