2016-04-17 17 views
2

Ich habe eine globale Variable inext richtig auf einen zufälligen Wert vor einem Aufruf von setchesQuestion() gesetzt. Sobald ich setWordsQuestion() eingegeben habe, ist der Wert 0. Wissen Sie, wie ich das debuggen kann?Globale Variable zurückgesetzt auf 0

enter image description here

und auf 0 zurückgesetzt:

enter image description here

Hier ist die Java-Datei:

public class ExercisesWords_fragment extends Fragment { 
static Context context; 
String login; 
String fileMenuCard = null; 

InputStreamReader isr; 
InputStream fIn; 
BufferedReader input_card = null; 

int level, lang; 
int ihelp; 

String fileLogin; 

RadioGroup rg; 
RadioButton rb1, rb2, rb3, rb4; 
EditText edt; 
TextView txt, question; 
Button validation, next, help; 

int MAXWORDS = 42; 
String[] words_questions = new String[MAXWORDS]; 
String[] words_cb1 = new String[MAXWORDS]; 
String[] words_cb2 = new String[MAXWORDS]; 
String[] words_cb3 = new String[MAXWORDS]; 
String[] words_cb4 = new String[MAXWORDS]; 
String[] words_response = new String[MAXWORDS]; 
int[] words_level = new int[MAXWORDS]; 

int MAXTEMP = 5; 
int[] tmp; 

int inext_question, inext; 

public ExercisesWords_fragment() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    return inflater.inflate(R.layout.exercises_words_fragment, container, false); 
} 

@Override 
public void onAttach(Activity activity){ 
    super.onAttach(activity); 
} 

@Override 
public void onPause() { 
    closeFileWords(); 
    super.onPause(); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    context = getActivity().getApplicationContext(); 

    fileMenuCard = "words"; 
    login = getArguments().getString("login"); 
    lang = getArguments().getInt("lang"); 
    level = getArguments().getInt("level"); 

    fileLogin = login + "_words.txt"; 

    initViewFind(); 
    initExerciseWords(); 
    nextWordsQuestion(); 

    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

    edt.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
      if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) { 
       validateResponse(); 
      } 
      return false; 
     } 
    }); 

    validation.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      validateResponse(); 
     } 
    }); 

    next.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      nextWordsQuestion(); 
     } 
    }); 

    help.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      ihelp = 1; 
      hideKeyboard(); 
      helpWords(); 
      ihelp = 0; 
     } 
    }); 


} 

private void drawBTN() { 
    if(lang == 0) { 
     validation.setText(R.string.pro_validation); 
     validation.setVisibility(View.VISIBLE); 
     next.setText(R.string.next); 
     next.setVisibility(View.VISIBLE); 
     help.setText(R.string.help); 
     help.setVisibility(View.VISIBLE); 
    } 
    else { 
     validation.setText(R.string.pro_validation_fr); 
     validation.setVisibility(View.VISIBLE); 
     next.setText(R.string.next_fr); 
     next.setVisibility(View.VISIBLE); 
     help.setText(R.string.help_fr); 
     help.setVisibility(View.VISIBLE); 
    } 
} 



private void nextWordsQuestion() { 
    int inext; 
    Random r; 
    r = new Random(); 
    inext = r.nextInt(MAXWORDS); 

    if (inext_question < MAXTEMP) { 
     tmp[inext_question] = inext; 
     inext_question++; 

     setWordsQuestion(); 
     return; 
    } 

    for (int i = 0; i < MAXTEMP; i++) { 
     if (inext == tmp[i]) { 
      r = new Random(); 
      inext = r.nextInt(MAXWORDS); 
      i--; 
     } 
    } 

    System.arraycopy(tmp, 1, tmp, 0, MAXTEMP - 1); 

    tmp[MAXTEMP - 1] = inext; 

    setWordsQuestion(); 
} 

private void setWordsQuestion(){ 
    question.setText(words_questions[inext]); 
    question.setVisibility(View.VISIBLE); 
    edt.setText(""); 

    rb1.setText(words_cb1[inext]); 
    rb2.setText(words_cb2[inext]); 
    rb3.setText(words_cb3[inext]); 
    rb4.setText(words_cb4[inext]); 

    rb1.setVisibility(View.INVISIBLE); 
    rb2.setVisibility(View.INVISIBLE); 
    rb3.setVisibility(View.INVISIBLE); 
    rb4.setVisibility(View.INVISIBLE); 
    rg.clearCheck(); 

    hideKeyboard(); 

} 

private void validateResponse(){ 
    String response; 

    if(ihelp == 1){ 
     int sel = rg.getCheckedRadioButtonId(); 
     if(sel < 0) return; 
     RadioButton rb = (RadioButton) getActivity().findViewById(sel); 
     response = rb.getText().toString(); 
    } 
    else { 
     response = edt.getText().toString(); 
    } 
    TextView resp = (TextView) getActivity().findViewById(R.id.resp_words); 
    if(response.equals(words_response[inext])) { 

     next.setTextColor(Color.GREEN); 
     next.setVisibility(View.VISIBLE); 

     resp.setText(R.string.resp_card); 
     resp.setTextColor(Color.GREEN); 
     resp.setVisibility(View.VISIBLE); 

    } 
    else { 
     resp.setText(R.string.resp_card_no); 
     resp.setTextColor(Color.RED); 
     resp.setVisibility(View.VISIBLE); 
    } 

} 

private void helpWords(){ 
    hideKeyboard(); 

    rb1.setText(words_cb1[inext]); 
    rb1.setVisibility(View.VISIBLE); 

    rb2.setText(words_cb2[inext]); 
    rb2.setVisibility(View.VISIBLE); 

    rb3.setText(words_cb3[inext]); 
    rb3.setVisibility(View.VISIBLE); 

    rb4.setText(words_cb4[inext]); 
    rb4.setVisibility(View.VISIBLE); 

} 

private void hideKeyboard(){ 
    EditText e = (EditText) getActivity().findViewById(R.id.words_answer); 
    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(e.getWindowToken(), 0); 
} 

private void openWords(){ 
    String l = null; 
    try { 
     if (lang == 0) 
      fIn = context.getResources().getAssets() 
        .open(fileMenuCard+".txt", Context.MODE_PRIVATE); 
     else 
      fIn = context.getResources().getAssets() 
        .open(fileMenuCard+"_fr.txt", Context.MODE_PRIVATE); 
     isr = new InputStreamReader(fIn); 
     input_card = new BufferedReader(isr); 
    } catch (Exception e) { 
     e.getMessage(); 
    } 

    for(int i=0;i<MAXWORDS;i++){ 
     try { 
      l = input_card.readLine(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     if (l != null) { 
      StringTokenizer tokens = new StringTokenizer(l, ";"); 
      words_questions[i] = tokens.nextToken(); 
      words_level[i] = Integer.valueOf(tokens.nextToken()); 
      words_cb1[i] = tokens.nextToken(); 
      words_cb2[i] = tokens.nextToken(); 
      words_cb3[i] = tokens.nextToken(); 
      words_cb4[i] = tokens.nextToken(); 
      words_response[i] = tokens.nextToken(); 
     } 
    } 
} 

private void closeFileWords(){ 
    try { 
     fIn.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     isr.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     input_card.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private void initExerciseWords(){ 
    tmp = new int[MAXTEMP]; 

    for(int i=0;i<MAXTEMP;i++) 
     tmp[i] = -1; 

    inext_question = 0; 

    openWords(); 

} 

private void initViewFind(){ 
    edt = (EditText) getActivity().findViewById(R.id.words_answer); 
    txt = (TextView) getActivity().findViewById(R.id.title_words); 
    validation = (Button) getActivity().findViewById(R.id.validation_words); 
    next = (Button) getActivity().findViewById(R.id.next_words); 
    help = (Button) getActivity().findViewById(R.id.help_words); 
    question = (TextView) getActivity().findViewById(R.id.words_question); 
    rg = (RadioGroup) getActivity().findViewById(R.id.rg_words); 
    rb1 = (RadioButton) getActivity().findViewById(R.id.radio1_words); 
    rb2 = (RadioButton) getActivity().findViewById(R.id.radio2_words); 
    rb3 = (RadioButton) getActivity().findViewById(R.id.radio3_words); 
    rb4 = (RadioButton) getActivity().findViewById(R.id.radio4_words); 

    if(lang == 0) 
     txt.setText(R.string.words_fr); 
    else 
     txt.setText(R.string.words_title_fr); 

} 

}

Antwort

2

Sie eine lokale Variable Einstellung, die die versteckt Instanzmitglied:

private void nextWordsQuestion() { 
    int inext; // remove this line 
    ... 
    inext = r.nextInt(MAXWORDS); 
    ... 
    setWordsQuestion(); 
    ... 
} 

Daher setWordsQuestion, die die Instanz Mitglied verwendet, finden Sie nicht den Wert, den Sie in nextWordsQuestion gesetzt.

0

Wenn Sie inext einen zufälligen Wert zuweisen, weisen Sie ihn einer lokalen Variablen zu, nicht der globalen.

Um zu beheben, entfernen Sie einfach die lokale Variable.

private void nextWordsQuestion() { 
//int inext; 
Random r; 
r = new Random(); 
inext = r.nextInt(MAXWORDS); 

if (inext_question < MAXTEMP) { 
    tmp[inext_question] = inext; 
    inext_question++; 

    setWordsQuestion(); 
    return; 
} 
+1

Bummer! Ich muss Staub in meinen Augen gehabt haben! – narb