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

时间:2014-11-20 20:57:01

标签: android multithreading crash

我的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手机上启动就好了。但是,只要我点击按钮buttbutt的颜色就会变为黑色,应用程序会立即崩溃,而不会在Service中运行循环。

我哪里错了?请帮忙。我真的想看看线程如何帮助完成这些工作并不断更改GUI,这可能有助于我在某一天的游戏开发中。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您应该使用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方法。