数组数据消失 - Android Studio

时间:2015-12-02 22:16:42

标签: java android arrays parse-platform

我试图从后端获取数据(我使用parse.com),将其放入数组并使用它通过适配器填充ListView。我将多个对象解析为一个列表,然后将" names"和" ids"分成两个单独的字符串数组。

现在,我的问题是,只要我将下一个位置添加到数组,数据就会消失。我花了很长时间用调试日志来解决这个问题,看起来好像在我写完后请求时正确显示了名称[0]。如果我在写名[1]后尝试访问名称[0],我会得到一个空指针异常 - println需要一条消息,应用程序崩溃。当不尝试通过调试日志访问数据时,listview由适当数量的条目填充,但应该来自names []和ids []的数据为空。这里的代码是一切都可怕的错误:

 names =  new String[2];
    ids =  new String[2];
    ParseQuery<ParseObject> query = ParseQuery.getQuery("TestItem");

    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> objects, ParseException e) {

            if (e == null) {
                int size = objects.size();
                int i = 0;
                ParseObject obj;

                while (i < size) {
                obj = objects.get(i);
                names[i] = obj.getString("name");
                ids[i] = obj.getString("objectId");
                i++;
                }
                /*The following code is fairly irrelevant(I think?) since the error appears
                to be somewhere in the previous lines.*/
                Bundle bundel = new Bundle();
                bundel.putStringArray("names", names);
                bundel.putStringArray("ids", ids);
                ft = getSupportFragmentManager().beginTransaction();
                screen = new Feed().newInstance(bundel);
                ft.add(R.id.holder_frame, screen);
                ft.commit();
            } else {

            }

        }
    });

我觉得这是管理数组的基本功能,但我似乎无法理解它。请帮忙:(

2 个答案:

答案 0 :(得分:0)

以下几行似乎很可疑:

new Feed().newInstance(bundel);

newInstance方法应该是静态方法。 确保使用捆绑信息添加适当的片段实例。如果您没有弄明白,请在片段中提供代码。

考虑以下代码:

ParseQuery<ParseObject> query = ParseQuery.getQuery("TestItem");
query.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> objects, ParseException e) {

        if (e == null) {
            int size = objects.size();

            String[] names =  new String[size];
            String[] ids =  new String[size];

            for (int i = 0; i < size; i++) {
                ParseObject elem = objects.get(i);
                names[i] = elem.getString("name");
                ids[i] = elem.getObjectId();
            }

            /*The following code is fairly irrelevant(I think?) since the error appears
            to be somewhere in the previous lines.*/
            Bundle params = new Bundle();
            params.putStringArray("names", names);
            params.putStringArray("ids", ids);

            ft = getSupportFragmentManager().beginTransaction();

            screen = new Feed();
            screen.setArguments(params);

            ft.add(R.id.holder_frame, screen);
            ft.commit();
        } else {
            Toast.make(<Current>Activity.this, "Error", ...).show();
        }
    }
});

答案 1 :(得分:-1)

我认为这可能是问题所在:

public static final Object socketLock = new Object();
public static DatagramSocket socket;
public static ArrayList<String> request = new ArrayList<>();
public static int iQueuePointer;
public static boolean bWait;

public static void main(String[] args) {
    new Main();
}

private Main() {

    try {
        socket = new DatagramSocket();
    } catch (SocketException e) {
        e.printStackTrace();
    }
    new Thread(receiveRunnable).start();
    new Thread(sendRunnable).start();
    iQueuePointer++;
    request.add("/info");
    iQueuePointer++;
    request.add("/ch/01/config/name");
    iQueuePointer++;
    request.add("/ch/02/config/name");
    iQueuePointer++;
    request.add("/ch/03/config/name");
    iQueuePointer++;
    request.add("/ch/04/config/name");
    iQueuePointer++;
    request.add("/ch/05/config/name");
    iQueuePointer++;
}

private Runnable sendRunnable = new Runnable() {
    @Override
    public void run() {
        System.out.println("Sending requests");
        while(socket != null) {
            if(!bWait && request.size() > 0 && request.size() < iQueuePointer) {
                iQueuePointer--;
                bWait = true;
                System.out.println("Servicing request " + iQueuePointer);
                //synchronized (socketLock) {
                    try {
                        socket.send(new DatagramPacket(request.get(0).getBytes(),
                                request.get(0).getBytes().length,
                                InetAddress.getByName("192.168.0.180"), 10024));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                //}
            }
        }
        System.out.println("sendRunnable ended");
    }
};

private Runnable receiveRunnable = new Runnable() {

    @Override
    public void run() {
        System.out.println("Listening for responses");
        byte[] data = new byte[1024];
        DatagramPacket packet = new DatagramPacket(data, data.length);
        while(socket != null) {
            //synchronized (socketLock) {
                try {
                    //socket.setSoTimeout(5000);
                    socket.receive(packet);
                    if (packet.getLength() > 0 && socket != null) {
                        System.out.println(new String(Arrays.copyOf(packet.getData(), packet.getLength()))
                                + " from " + iQueuePointer);
                        request.remove(0);
                        System.out.println("Removed " + iQueuePointer);
                        bWait = false;
                    }
                } catch (Exception e) {
                    if (!e.toString().contains("Receive timed out")) {
                        e.printStackTrace();
                    } else {
                        System.out.println("Receive loop - bWait=" + bWait + " request.size=" + request.size()
                                + " iQueuePointer=" + iQueuePointer);
                    }
                }
            //}
        }
        System.out.println("receiveRunnable ended");
    }
};

名称和ID初始化为

int size = objects.size();
int i = 0;
ParseObject obj;

while (i < size) {
  obj = objects.get(i);
  names[i] = obj.getString("name");
  ids[i] = obj.getString("objectId");
  i++;
}

您正在遍历 names = new String[2]; ids = new String[2]; 中的所有元素,因此如果它有两个以上的元素,您将在objectsnames数组的边界之外编写。