gpt4 book ai didi

java - 在 AsyncTask 中填充数组时出现 Android 错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:30:53 25 4
gpt4 key购买 nike

我想填充一个数组,以便我可以将该结果附加到 TextView,但应用程序一直崩溃:

04-02 23:45:29.286: E/AndroidRuntime(6620): FATAL EXCEPTION: AsyncTask #3
04-02 23:45:29.286: E/AndroidRuntime(6620): java.lang.RuntimeException: An error occured while executing doInBackground()
04-02 23:45:29.286: E/AndroidRuntime(6620): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-02 23:45:29.286: E/AndroidRuntime(6620): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
04-02 23:45:29.286: E/AndroidRuntime(6620): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
04-02 23:45:29.286: E/AndroidRuntime(6620): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
04-02 23:45:29.286: E/AndroidRuntime(6620): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-02 23:45:29.286: E/AndroidRuntime(6620): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-02 23:45:29.286: E/AndroidRuntime(6620): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-02 23:45:29.286: E/AndroidRuntime(6620): at java.lang.Thread.run(Thread.java:1019)
04-02 23:45:29.286: E/AndroidRuntime(6620): Caused by: java.lang.NullPointerException
04-02 23:45:29.286: E/AndroidRuntime(6620): at com.gettford.community.MessageActivity$Mensajes.doInBackground(MessageActivity.java:166)
04-02 23:45:29.286: E/AndroidRuntime(6620): at com.gettford.community.MessageActivity$Mensajes.doInBackground(MessageActivity.java:1)
04-02 23:45:29.286: E/AndroidRuntime(6620): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-02 23:45:29.286: E/AndroidRuntime(6620): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-02 23:45:29.286: E/AndroidRuntime(6620): ... 4 more

我有两个字符串:

public String[] mensa;
public String[] NomUsuario;

然后我在 doInBackround 中执行 AsynTask:

// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://www.gettford.net/comunidad/api/chat_conv.php?user="+usuarioID+"&id="+idsala);

try {
// Locate the array name in JSON

jsonarray = jsonobject.getJSONArray("lista");

for (int i = 0; i < jsonarray.length(); i++) {
//HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);

// Retrive JSON Objects
//usuario = jsonobject.getString("Nombre");
mensa[i] = jsonobject.getString("mensaje");
NomUsuario[i] = jsonobject.getString("NomUsuario");
Log.i("mensaje",mensa[i]);

}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void args) {
lblMessage.setText("");
for (int i= 0; i < mensa.length; i++){
lblMessage.append(NomUsuario[i]+": "+mensa[i] + "\n");
}
}

报错的是这一行:

mensa[i] = jsonobject.getString("mensaje");

感谢帮助

谢谢!

最佳答案

您只是忘记了初始化数组变量。在 try block 开始时,您需要像下面这样初始化 Array 变量,

try {
// Locate the array name in JSON
mensa = new String[jsonarray.length()]; // Add this line.
NomUsuario = new String[jsonarray.length()]; // Add this line.

关于java - 在 AsyncTask 中填充数组时出现 Android 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22827819/

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