2015-08-28 93 views
2

我通过一个意图传递一个字符串数组列表,当我从捆绑中获得它,我得到一个空值。你知道错误是什么吗?是因为我在newMyFriendRequest方法中使用它吗?Android:通过意图传递字符串ArraryList。价值收到null

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

回答

0

你应该把这个代码:右后

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

intent.putExtras(extras); 

startActivity(intent);` 

您的内部GraphRequestBatch循环,因为根据开发者网站,

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

数据甚至存在于意图之前,下一个活动已经启动..

这应该工作,但没试过这种自己。

+0

感谢它的工作 – akalanta

+0

很高兴帮助..! –

相关问题