gpt4 book ai didi

java - 静态变量没有改变并被替换为 ""

转载 作者:行者123 更新时间:2023-12-01 18:04:19 27 4
gpt4 key购买 nike

我的应用程序是一个简单的单词拼图游戏,因此其级别是使用 AsyncTask 从 Json Web 服务获取的。 I类可以获取doInBackground中的数据和onPostExecute方法,我在 FetchData 中使用局部变量类来保存获取的数据,数据只是 6 个字符串,即图像 URL 和级别 id,以及用于按钮的 4 个单词,here是游戏界面,可以看到有4个按钮,每个按钮都有一个单词,玩家必须通过看图片找到它。

因此,当玩家找到例如 1 个单词并离开应用程序并关闭它时,必须保存玩家找到的单词,并且当他返回到 LevelActivity 时他应该找到更多 3 个单词来继续关卡。

问题:是,当我找到一个单词并且该单词在我关闭应用程序并返回 this 时显示(因此必须保存)时发生这种情况,根据我的测试,我发现那些影响数据滞后的代码行

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.

使用的数据获取方式: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() {

}
});
}

}

感谢您查看我的答案:D告诉我您是否需要任何其他代码。

最佳答案

onPostExecute() 调用结束时

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

失踪了。因此,按钮无法知道新值,因此保持为空。

关于java - 静态变量没有改变并被替换为 "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60581862/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com