Android线程异常

时间:2012-12-14 07:10:53

标签: android

我在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;

1 个答案:

答案 0 :(得分:1)

使用

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

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