-1

I'd like to get color value inside void run, how can I do this? any example? color is null there.

   public void onResponse(Call call, final Response response) throws IOException {
        String color = response.body().string();
        Profile.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                setTheme(color); //color is null here.
            }
        }
    }

color is null inside run() full code:

   protected void onCreate(Bundle savedInstanceState) {

    this.context = getApplicationContext();
    OkHttpClient client = new OkHttpClient();
    okhttp3.Request request = new okhttp3.Request.Builder()
            .url("http://ip/color.php")
            .build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }
        @Override
        public void onResponse(Call call, final Response response) throws IOException {
            String color = response.body().string();
            Profile.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    setTheme(color);
                }
            }
        }
    });...
renata costa
  • 177
  • 10

2 Answers2

1

If it says variable color is accessed from within inner class, make the variable color as Global. Declare it outside your onCreate.

Or make it final

final String color = response.body().string();
Asutosh Panda
  • 1,463
  • 2
  • 13
  • 25
1

First of all your code shouldn't compile as you cannot access non final variable inside of "closure". So the only change you need is

final String color = response.body().string();
Profile.this.runOnUiThread(new Runnable() {
     @Override
     public void run() {
          setTheme(color);
     }
}
j2ko
  • 2,479
  • 1
  • 16
  • 29