更改背景颜色不起作用

时间:2016-12-05 04:32:22

标签: java android timer background-color

我想在500毫秒后获得点击更改的背景。它现在所做的一切都是黑色的。它甚至不会将视图显示为黑屏。颜色列表来自数据库,JSON数据被解析为颜色值。我使用的是Android 6 API 23。

public class CycleColors extends AppCompatActivity {

    private static final String TAG     =  CycleColors.class.getName();

    static List<String> colorsList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cycle_colors);

        Button doneBtn  = (Button)findViewById(R.id.btnDone);

        doneBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                clickDoneButton();
            }
        });

        getData();
        Intent intent   = getIntent();
        new cycle(this).execute();

    }

    public void clickDoneButton()
    {
        finish();
    }

    class cycle extends AsyncTask<Void, Void, Void>
    {
        private Activity    activity;
        View                view;

        public cycle(Activity a)
        {
            activity    = a;
            view        = activity.getWindow().getDecorView();
        }

        protected Void doInBackground(Void... param)
        {
            activity.runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    while(true)
                    {
                        for (String c: colorsList)
                        {
                            int color   = Color.parseColor(c);
                            Log.d(TAG, color+"");
                            view.setBackgroundColor(color);
                            SystemClock.sleep(500);
                        }
                    }
                }
            });
            Log.d(TAG, "returned null");
            return null;
        }
    }



    private void getData() {

        final String            serverURL;
        final JsonArrayRequest  request;
        final RequestQueue      queue;

        serverURL   = "https://api.mlab.com/api/1/databases/comp3717final/collections/colours?apiKey=qR2ag5UaRrHBxDm6KEyg95EESmfY5Bcf";
        queue       = Volley.newRequestQueue(this);
        request     = new JsonArrayRequest(serverURL,
                new onJSONResponse(),
                new onJSONError());
        queue.add(request);
    }

    private class onJSONResponse implements Response.Listener<JSONArray>
    {
        @Override
        public void onResponse(JSONArray response)
        {
            final int length;
            int i;
            i = 0;
            length = response.length();

            try {
                for (; i < length; i++) {
                    final JSONObject colorObject;
                    final JSONObject hexObject;
                    final String colorName;
                    final String hexCode;

                    colorObject = response.getJSONObject(i);
                    colorName = colorObject.getString("color");
                    hexCode = colorObject.getString("value");
                    Log.d(TAG, colorName + " => " + hexCode);
                    colorsList.add(colorName);
                }
            } catch (final JSONException ex) {
                Log.d(TAG, "Error getting json object: " + i, ex);
                colorsList.clear();
            }
            Log.d(TAG, "working");
        }
    }

    private static class onJSONError implements Response.ErrorListener
    {
        @Override
        public void onErrorResponse(VolleyError error)
        {
            Log.d(TAG, "JSON ERROR");
        }
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.peymantp.androidfinal.CycleActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Done"
        android:id="@+id/button6"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="32dp" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

getData,它同时发生网络调用和AsyncTask执行,这可能会在asyncTask完成后导致colorList初始化。您需要在AsysncTask访问colorList之前以同步方式调用getData。