2015-09-14 61 views
-2

我拉動一些JSON響應,並根據是否有響應中的某個對象,我想要去一個活動或其他。但是,即使我檢查JSON對象的id的條件是有效的,它也不會進入if語句中的活動。爲什麼我的活動一旦達到if陳述中的startActivity就不會終止?Android沒有執行意圖的循環

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       JSONObject passingObject = modelsObjectList.get(position); 
       ArrayList<String> nextPlacesList = new ArrayList<>(); 

       try { 


        if(passingObject.optJSONArray("places") == null){ 
         Log.v("WE ARE IN THE LOOP", "in the loop"); 
         name = passingObject.getString("name"); 
         id = passingObject.getLong("id"); 
         Log.d("NEXT ACTIVITY NAME", name); 
         System.out.println("NEXT ACTIVITY ID " + id); 
         Log.d("NEXT SERIES LIST", nextPlacesList.toString()); 

         Intent intent = new Intent(SeriesActivity.this, SecondSeriesActivity.class); 
         intent.putExtra("id", id); 
         intent.putExtra("name", name); 
         intent.putExtra("placesList", nextPlacesList); 
         startActivity(intent); 
        } 

        Log.v("SHOULDN'T BE HERE", "this is bad"); 
        JSONArray places = passingObject.getJSONArray("places"); 
        for(int i = 0; i < places.length(); i++){ 
         nextPlacesList.add(places.getString(i)); 

        } 

        name = passingObject.getString("name"); 
        id = passingObject.getLong("id"); 
        Log.d("NEXT ACTIVITY NAME", name); 
        System.out.println("NEXT ACTIVITY ID " + id); 
        Log.d("NEXT SERIES LIST", nextPlacesList.toString()); 

       } catch(JSONException e){ 
        Log.d("JSONEXCEPTION", e.getMessage()); 
       } 

       Intent intent = new Intent(SeriesActivity.this, PlacesListActivity.class); 
       intent.putExtra("id", id); 
       intent.putExtra("name", name); 
       intent.putExtra("placesList", nextPlacesList); 
       startActivity(intent); 
      } 
} 
+0

將調用完成()幫助? – Ramesh

+0

java 101:一個函數執行,直到它達到3件事之一:一個異常,一個返回語句,功能塊的結束。 – njzk2

回答

2

startActivity不停止執行該方法。您需要在if塊結束之前有return以防止它繼續到SHOULDNT BE HERE部分。