简体   繁体   English

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

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

I am passing a String array list through an intent and when I get it from the bundle I get a null value. 我通过意图传递了一个String数组列表,当我从包中获取它时,我得到了一个空值。 Do you know what the error is? 您知道错误是什么吗? Is it because I am using it inside the newMyFriendRequest method? 是因为我在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);

In the activity OnCreate Method: 在活动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);

You should put this code : 您应该输入以下代码:

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

 intent.putExtras(extras);

 startActivity(intent);`

right after your for loop inside GraphRequestBatch because according to the developer site, GraphRequestBatch的for循环之后,因为根据开发者网站,

executeAsync() - Executes this batch asynchronously. executeAsync() -异步执行此批处理。 This function will return immediately, and the batch will be processed on a separate thread. 该函数将立即返回,并且批处理将在单独的线程上进行处理。 In order to process results of a request, or determine whether a request succeeded or failed, a callback must be specified (see GraphRequest.setCallback(GraphRequest.Callback)). 为了处理请求的结果,或确定请求成功还是失败,必须指定回调(请参见GraphRequest.setCallback(GraphRequest.Callback))。

Before the data is even present in the intent, next Activity is already started.. 在意图中甚至没有数据之前,下一个活动已经开始。

This should work but didn't tried this myself. 这应该可以,但是我自己没有尝试过。

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

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