2014-11-03 80 views
-1

我真的不能看到我的代碼上的問題。
startActivity()方法在下面的代碼中不起作用。Android的startActivity()不適用於循環內

即使Toast.makeText()不起作用。
如果我在for循環之前放置了toast循環,但是我不能在for循環之前放置startActivity,因爲有錯誤。

//I think nothing special here 
    case R.id.bGeneral: 
     break; 
    case R.id.bProfessional: 
     type = "professional"; 
     new loadQuestioners().execute(type); 

     break; 

    @Override 
    protected void onPostExecute(JSONObject result) { 
     // TODO Auto-generated method stub 
     pDialog.dismiss(); 
     int success; 
     try { 
      success = result.getInt(TAG_SUC); 
      if (success == 0) { 
       Toast.makeText(getApplicationContext(), 
         result.getString("message"), Toast.LENGTH_LONG) 
         .show(); 
      } else if(success == 1){ 
       Toast.makeText(getApplicationContext(), result.getString("message"),     Toast.LENGTH_LONG).show(); //When I put it here it work 
       MyClass addQuest = new MyClass(); 
       JSONArray jarray = result.getJSONArray("assessments"); 
       for (int x = 0; x < jarray.length(); x++) { 
        JSONObject j = jarray.getJSONObject(x); 
        String question = j.getString("Ass_Quesion"); 
        String a = j.getString("Ass_Opt_A"); 
        String b = j.getString("Ass_Opt_B"); 
        String c = j.getString("Ass_Opt_C"); 
        String d = j.getString("Ass_Opt_C"); 
        String answer = j.getString("Ass_Answer"); 

        addQuest.addQuestion(question, a, b, c, d, answer);    

       } 

       //Toast is also not working here 

       Bundle assessmentType = new Bundle(); 
       Intent questioners = new Intent (Assessments.this, Questioners.class); 
       assessmentType.putString("assessmentType", type); 
       questioners.putExtras(assessmentType); 
       startActivity(questioners); //NOT WORKING HERE 
       finish(); 


      } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 

UPDATE THX對我的幫助我findout,我在這裏有沒有錯誤。我認爲這裏的錯誤是forloop沒有將問題添加到我的構造函數中,所以當我的Questioner類在onCreate上獲取ArrayList的索引時,它會發生錯誤。

public class MyClass { 

private static ArrayList<ArrayList<String>> questioners = new ArrayList<ArrayList<String>>(); 
private static int col = 0; 

public void addQuestion(String question, String a, String b, String c, String d, String answer){ 
    this.questioners.add(new ArrayList<String>()); 
    questioners.get(col).add(question); 
    questioners.get(col).add(a); 
    questioners.get(col).add(b); 
    questioners.get(col).add(c); 
    questioners.get(col).add(d); 
    questioners.get(col).add(answer); 
    col++; 
} 

public ArrayList<ArrayList<String>> getQuestion(){ 
    return this.questioners; 
} 

}

+0

請發佈嘗試結束{block – Michael 2014-11-03 18:24:13

+0

'assessmentType.putString(「assessmentType」,type);'......什麼是'type'?我在代碼中看不到它。 – 2014-11-03 18:26:06

+0

檢查jarray.length()> 0,否則不起作用。 – nunofmendes 2014-11-03 18:27:10

回答

0

檢查是否Assessments類擴展Context,並Questioners擴展Activity。另外請確保您已將Questioners活動添加到AndroidManifest.xml

+0

是的,他們都擁有。公共類評估擴展活動實現OnClickListener,公共類Questioners extends活動 – 2014-11-03 18:41:59

+0

你可以發佈你得到的錯誤嗎? – 2014-11-03 18:45:40

+0

我的錯誤是在我的構造函數for循環沒有添加從PHP的問題到我的構造〜_〜 – 2014-11-03 18:59:40