Android startActivity()不在for循环中工作

时间:2014-11-03 18:22:21

标签: android start-activity

我真的看不到代码上的问题 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();
        }
      }

更新 感谢我帮助我,发现我没有错误。我认为错误就在这里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;
}

}

1 个答案:

答案 0 :(得分:0)

检查Assessments类是否扩展ContextQuestioners扩展Activity。另外,请确保将Questioners活动添加到 AndroidManifest.xml