-2

我在android中同时执行两个线程时遇到问题。通过一个线程处理要在一定时间间隔后执行的方法,而另一个线程试图处理几个按钮上的 onClick 响应,但问题是只有一个线程正在执行,即调用该方法的线程。请给我解决方案。感谢你在期待。

Thread th=new Thread(){

            @Override
            public void run(){
                try
                {
                    for (timer = 0; timer < 1000; timer++)
                    {
                        int waited = 0;
                        while(waited < splashTime)
                        {
                            Thread.sleep(100);
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    try {
                                          ds.open();
                                          String               quotes=ds.getRandomQuote();
                                          textView.setText(quotes);
                                          ds.close();
                                        //textView.setText(num[timer]);

                                    }
                                    catch(Exception e)
                                    {
                                        e.printStackTrace();
                                    }
                                }
                            });
                            waited += 100;
                        }
                    }
                }catch (InterruptedException e) {
                }

            }
        };th.start 


private void quotes() {
    // TODO Auto-generated method stub
     for (timer = 0; timer < 1000; timer++)
     {
         int waited = 0;
         while(waited < splashTime)
         {
             try {
                  ds.open();
                  String quotes=ds.getRandomQuote();
                  textView.setText(quotes);
                  ds.close();


              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
         }
         waited+=100;
     }

}

private void getAllQuotesFromAssets() {
if(ds.getRandomQuotesList().size()==0)
{   
    new performBackgroundtask().execute();}


            }

Thread th1=new Thread()
{
  public void run()
  {  



public void myClickHandler(View view)
{


        switch (view.getId()) {

        case R.id.author_button:
            //Toast.makeText(getApplicationContext(), "author",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this,AuthorList.class);
            startActivity(intent);
            break;

        case R.id.category_button:
            //Toast.makeText(getApplicationContext(), "category",Toast.LENGTH_SHORT).show();
            Intent intent1 = new Intent(this,CategoryList.class);
            startActivity(intent1);
            break;


        case R.id.fev_list_button:
            //Toast.makeText(getApplicationContext(), "fevourite",Toast.LENGTH_SHORT).show();
            Intent intent2 = new Intent(this,fev.class);
            startActivity(intent2);
            break;


        case R.id.random_button:
            //Toast.makeText(getApplicationContext(), "random",Toast.LENGTH_SHORT).show();
            Intent intent3 = new Intent(this,Random_quotes.class);
            startActivity(intent3);
            break;

        default:
            break;
        }
    }

};
th1.start;
   th.join;
4

1 回答 1

1

采用

Your_Current_activity.this.runOnUiThread(new Runnable() {
    public void run() {
        // your UI code here
    });

因为runOnUiThread是 Activity 类的方法而不是 Thread

于 2012-12-14T07:21:28.723 回答