2013-02-17 63 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