-1

I really can't see the problem on my code.
The startActivity() method is not working in the code below.

Even Toast.makeText() does not work.
If I put the toast before the for-loop it works, but I cannot put startActivity before for-loop because there's a error.

    //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 for helping me I findout that I have no error here. I think the error is in here the forloop is not adding the question to my contructor so when my Questioner class get the index of ArrayList on onCreate it make an error.

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

}

  • please post the end of the try { block – Michael Nov 03 '14 at 18:24
  • `assessmentType.putString("assessmentType", type);`... What is `type`? I don't see it anywhere in the code. – Pedro Oliveira Nov 03 '14 at 18:26
  • Check if jarray.length() > 0, otherwise it will not work. – nunofmendes Nov 03 '14 at 18:27
  • there should an Exception in the for loop and exception is caught.As a result the code did not execute. – Meher Nov 03 '14 at 18:41
  • thx for suggesting to add Exception so know that I find out that I have a big problem haha. actually the error is on the next page Questioners It seems my question is not adding to my contructor myClass – Majikero Gallardo Nov 03 '14 at 18:57
  • If you are trying to start an Activity in a loop, you are probably doing things wrong. If that's really the behavior you need, you'll probably have to re-architect as something like a state machine which can remember what it should do next, and then do that when an event method next runs its code. – Chris Stratton Nov 03 '14 at 19:04
  • No sir Im use startActivity when the loops end to put all records to my contructor arraylist – Majikero Gallardo Nov 03 '14 at 19:08

1 Answers1

0

Check if the Assessments class extends Context, and Questioners extends Activity. Also make sure that you added the Questioners activity to the AndroidManifest.xml.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71