在Android上的线程中运行无限循环时,应用程序崩溃。

问题描述:

我有一个Android的服务如下:在Android上的线程中运行无限循环时,应用程序崩溃。

public class TestService extends Service { 

    . 
    . 
    . // Variables 
    . 
    private Thread ChangeColors; 
    private final int[] colors = {Color.BLACK, Color.RED, Color.YELLOW, Color.BLUE, Color.GREEN}; 

    @Override 
    public void onStartCommand(Intent intent, int flags, int startID) { 

     LinearLayout layout = TestActivity.getLayout(); // Returns the layout from the activity class 
     changeColors = new Thread() { 

      int index = 0; 


      @Override 
      public void run() { 

        while(!isInterrupted()) { 

         try { 

          layout.setBackgroundColor(colors[index]); // Set the background to the Color at the index'th position in the array. 
          index ++; // Increment the index count. This will throw ArrayIndexOutOfBoundsException once the index > colors.length 
         } catch(ArrayIndexOutOfBoundsException exception) { 

          index = 0; // Then simply set the value of index back to 0 and continue looping. 
         } 
        } 
      } 
     }; 

     changeColors.start(); 
     return START_STICKY; 
    } 

    . 
    . 
    Other methods 
    . 
    . 

    @Override 
    public void onDestroy() { 

     super.onDestroy(); 
     changeColors.interrupt(); 
     Toast.makeText(this, "Stopped!", Toast.LENGTH_SHORT).show(); 
    } 
} 

该服务只是获取android.widget.LinearLayout实例从Activity上的按钮,点击不断变化的LinearLayout的背景色。

这是活动:

public class TestActivity extends Activity { 


    private static LinearLayout layout; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     layout = new LinearLayout(this); 
     layout.setOrientation(LinearLayout.VERTICAL); 
     layout.setLayoutParams(new LinearLayout.LayoutParams(

      LinearLayout.LayoutParams.FILL_PARENT, 
      LinearLayout.LayoutParams.FILL_PARENT 
     )); 

     Button butt = new Button(this); 
     butt.setText("Start thread!"); 
     button.setLayoutParams(new LinearLayout.LayoutParams(

      LinearLayout.LayoutParams.FILL_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT 
     )); 


     layout.addView(butt); 
     setContentView(layout); 


     butt.setOnClickListener(

      new View.OnClickListener() { 

       @Override 
       public void onClick(View view) { 

        butt.setBackgroundColor(Color.BLACK); 
        startService(getContextBase(), TestService.class); 
       } 
      } 
     ); 

     // I have another button coded here which on clicking calls the stopService() method. 
    } 



    public static LinearLayout getLayout() { 

     return layout; 
    } 
} 

程序似乎是相当简单的。该应用在我的LG-L90手机上启动得很好。但只要我点击butt按钮,butt的颜色就会变为黑色,并且应用程序会立即崩溃,而不会在Service中运行循环。

我哪里错了?请帮忙。我真的想看看线程如何帮助我们做这些事情,并不断改变可能有助于我在游戏开发中的GUI。

在此先感谢。

+0

我认为你必须在UI线程中进行UI操作。将这部分代码放在'runOnUiThread'中。 – 2014-11-20 21:00:03

+0

你能为我写代码吗? M新的android。但是有了Java的经验。尽管如此,从未编写过多线程应用程序。 – 2014-11-20 21:02:04

您应该使用runOnUiThread进行UI操作。

public class MyService extends Service { 

    Handler handler; 

    public MyService() { 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     handler = new Handler(); 
    } 

    private void runOnUiThread(Runnable runnable) { 
     handler.post(runnable); 
    } 

    ... 
} 

,并确保您导入android.os.Handler,现在称之为runOnUiThread方法。

+0

runOnUiThread()是'android.app.Activity'中的一个方法。有没有什么办法可以从'android.app.Service'调用它? – 2014-11-21 06:58:17

+0

看到我更新的答案。你必须使用[Handler](http://developer.android.com/reference/android/os/Handler.html) – 2014-11-21 11:30:16

+0

号。它不工作 – 2014-11-25 09:47:48