【发布时间】:2020-03-07 20:18:59
【问题描述】:
我的应用程序是一个简单的单词拼图游戏,因此它的关卡是从 Json 网络服务中获取的,使用 AsyncTask 类我可以在 doInBackground 和 onPostExecute 方法中获取数据,我使用FetchData 类中的局部变量来保存获取的数据,数据只是 6 个字符串,即图像 URL 和关卡 id,以及 4 个用于按钮的单词,here 是游戏界面,如您所见有4个按钮,每个按钮都有一个单词,玩家必须通过查看图片找到它。
所以当玩家找到例如 1 个单词并离开应用程序并关闭它时,必须保存玩家找到的单词,当他返回 LevelActivity 时,他应该通过找到更多 3 个单词来继续关卡。
问题: 是当我找到一个单词并且该单词显示(因此必须保存)时,当我关闭应用程序并返回 this 时发生,这取决于我发现的测试那些影响数据滞后的代码行
注意:每当我(手动)重新加载活动时,一切都会变得好起来,而不是在重新创建活动后显示一个空按钮。
使用的数据获取方式: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() {
}
});
}
}
感谢您查看我的回答:如果您需要任何其他代码,请告诉我。
【问题讨论】:
-
看起来您再次从网络上获取单词,一旦应用程序再次启动。将整个进度数据保存到数据库中,然后在再次启动应用程序后从那里获取数据。
-
有没有其他方法,因为我真的没有足够的时间做数据库,我正在寻找一个单一的解决方案或者更简单的方法,因为问题不是那么大,我可以只要重新创建()我的活动,只要它打开但它不起作用。感谢您帮助我!
-
在这种情况下,需要更多信息/代码。你什么时候开始获取数据?获取数据时你会做什么?
-
我添加了 FetchData 类代码及其使用位置。 :D
-
在
onPostExecute()末尾添加调用:checkButton1()checkButton2()checkButton3()checkButton4()