繁体   English   中英

Android:通过意图传递字符串ArraryList。 收到的值为空

[英]Android: Passing String ArraryList through intent. Value received null

我通过意图传递了一个String数组列表,当我从包中获取它时,我得到了一个空值。 您知道错误是什么吗? 是因为我在newMyFriendRequest方法中使用它吗?

            final Bundle extras = new Bundle();

            extras.putString(EXTRA_PROFILENAME, profile.getFirstName());
            extras.putString(EXTRA_PROFILEID, profile.getId());

            final ArrayList<String> listids = new ArrayList<String>();
            final ArrayList<String> listnames = new ArrayList<String>();


            GraphRequestBatch batch = new GraphRequestBatch(
                    GraphRequest.newMyFriendsRequest(accessToken, new GraphRequest.GraphJSONArrayCallback() {
                                @Override
                                public void onCompleted(JSONArray jsonArray, GraphResponse response) {
                                    // Application code for users friends
                                    System.out.println("getFriendsData onCompleted : jsonArray " + jsonArray);
                                    System.out.println("getFriendsData onCompleted : response " + response);
                                    try {

                                        for(int i=0; i<jsonArray.length(); i++)
                                        {
                                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                                            listids.add(jsonObject.getString("id"));
                                            listnames.add(jsonObject.getString("name"));
                                            System.out.println("Iteration: " + i);
                                            System.out.println("Name: "+jsonObject.getString("name") );
                                            System.out.println("ID: "+jsonObject.getString("id") );

                                        }

                                        extras.putStringArrayList("names", listnames);
                                        extras.putStringArrayList("ids", listids);

                                        System.out.println("Names: " + listnames);
                                        System.out.println("IDs: " + listids);

                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            })

            );

            batch.addCallback(new GraphRequestBatch.Callback() {
                @Override
                public void onBatchCompleted(GraphRequestBatch graphRequests) {
                    // Application code for when the batch finishes
                }
            });
            batch.executeAsync();

            Intent intent = new Intent(getActivity(), MainLobby.class);

            intent.putExtras(extras);

            startActivity(intent);

在活动OnCreate方法中:

    intent = getIntent();

    Bundle extras =  intent.getExtras();
    //value is null not passing correctly
    listids = extras.getStringArrayList("ids");
    listnames = extras.getStringArrayList("names");

     System.out.println("MainLobby");
    System.out.println("Name: " + listnames);   
    System.out.println("User Logged In: " + user_name);

您应该输入以下代码:

`Intent intent = new Intent(getActivity(), MainLobby.class);

 intent.putExtras(extras);

 startActivity(intent);`

GraphRequestBatch的for循环之后,因为根据开发者网站,

executeAsync() -异步执行此批处理。 该函数将立即返回,并且批处理将在单独的线程上进行处理。 为了处理请求的结果,或确定请求成功还是失败,必须指定回调(请参见GraphRequest.setCallback(GraphRequest.Callback))。

在意图中甚至没有数据之前,下一个活动已经开始。

这应该可以,但是我自己没有尝试过。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM