2015-08-28 48 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

很高興幫助..! –