简体   繁体   中英

Static variable doesn't change and get replaced with " "

My app is a game that is a simple words puzzle, so the levels on it is fetched from a Json web service, using AsyncTask class I can fetch the data in doInBackground and onPostExecute methods, I use local variables in FetchData class to hold the fetched data in, the data is simply 6 strings that are an image URL and level id, and 4 words that is for the buttons, here is the game interface, as you can see there are 4 buttons each one has a word and the player must find it by looking at the picture.

So when the player finds for example 1 word and leaves the app and closes it, the word the player found must be saved and when he return back to LevelActivity he is supposed to continue the level by finding more 3 words.

THE PROBLEM: is that when I find a word and the word shows (so it must be saved) when I close the app and return this happens, depending on my testing I found out that those lines of code that effects the data lag

NOTE: That whenever I reload the activity (manually) everything gets good and instead of having an empty button after recreating the activity the word shows.

data fetching method used: in onCreate & onResume

//This is in LevelActivity.java:

//These methods checks if the button is answered previously or not (button1/button2... variables are true when a word is answered)

  public void checkButton1() {

        if (button1) {
            wordButton1.setText(button1Word); //<--- Here if I changed it to .setText("Test") 
          //the lag will disappear and the button will show "Test" (without the quotation) and everything's good
          //So the problem is when I use .setText(button1Word); that is the word fetched from Json web service.
          //it doesn't throw NullPointerException and it doesn't show the word
          //but what? it set the text to " "? Why?

          //Note other buttons are the same thing too

        }

    }

    public void checkButton2() {

        if (button2) {
            wordButton2.setText(button2Word);
        }
    }

    public void checkButton3() {

        if (button3) {
            wordButton3.setText(button3Word);
        }
    }

    public void checkButton4() {

        if (button4) {
            wordButton4.setText(button4Word);
        }
    }

//This is FetchData class that fetches the data from Json web service (full code)

package com.example.wordspuzzlejsontest;

import android.content.Context;
import android.os.AsyncTask;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class FetchData extends AsyncTask<Void, Void, Void> {

//Local variable that are used to hold fetched data to transfer them to LevelActivity with static variables
    static int currentLevel = 0;
    String w1;
    String w2;
    String w3;
    String w4;
    String data = "";
    String id;
    String img;
    Context context;

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL("https://api.jsonbin.io/b/5e42776dd18e4016617690ce/7");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while (line != null) {
                line = bufferedReader.readLine();
                data = data + line;
            }

            JSONArray JA = new JSONArray(data);

            JSONObject JO = (JSONObject) JA.get(currentLevel);

            id = (String) JO.get("id");
            img = (String) JO.get("img");
            w1 = (String) JO.get("w1");
            w2 = (String) JO.get("w2");
            w3 = (String) JO.get("w3");
            w4 = (String) JO.get("w4");


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        int levelId = Integer.parseInt(id);
        levelId++;

        //Loading the words data to buttons
        LevelActivity.levelID = String.valueOf(levelId);
        LevelActivity.imageURL = img;
        LevelActivity.button1Word = w1;
        LevelActivity.button2Word = w2;
        LevelActivity.button3Word = w3;
        LevelActivity.button4Word = w4;

        //Loading level image and level number on the screen
        LevelActivity.levelIdTextView.setText(LevelActivity.levelID);
        loadLevelImage();
    }

    public void loadLevelImage() {

        Picasso.with(context).load(LevelActivity.imageURL).placeholder(R.drawable.loading)
                .error(R.drawable.loading)
                .into(LevelActivity.imageView, new com.squareup.picasso.Callback() {

                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError() {

                    }
                });
    }

}

Thanks for viewing my answer :D tell me if you need any other code.

At the end of onPostExecute() calls to

checkButton1()
checkButton2()
checkButton3()
checkButton4()

are missing. Because of that, buttons cannot know about the new values and therefore remain empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM