java - doinbackground - asynctask方法



在AsyncTask中填充数组时出现Android错误 (3)

你只是忘了初始化你的数组变量。 在try块的开始处,你需要像下面那样初始化你的Array变量,

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

https://src-bin.com

我想填充一个数组,所以我可以附加到一个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;

那么我做的AsynTask,在doInBackround:

// 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");

感谢帮助

谢谢!


Answer #1

你还没有初始化你的任何数组。

jsonarray = jsonobject.getJSONArray("lista");

if (jsonarray.length() > 0) {
// Initialized both array if the size of that JSONArray is grater then 0
mensa = new String[jsonarray.length()]
NomUsuario= new String[jsonarray.length()]

    for (int i = 0; i < jsonarray.length(); i++) {
        jsonobject  = jsonarray.getJSONObject(i);
        mensa[i] = jsonobject.getString("mensaje");
        NomUsuario[i] = jsonobject.getString("NomUsuario");
        Log.i("mensaje",mensa[i]);
    }
}

Answer #2

您应该在for循环之前添加mensa = new String[jsonarray.length()]