2016-04-17 11 views
2

setWordsQuestion()を呼び出す前に、グローバル変数inextをランダム値に正しく設定しました。 setWordsQuestion()を入力すると値は0になります。これをどのようにデバッグできるか知っていますか?グローバル変数を0にリセット

enter image description here

と0にリセット:あなたが隠しローカル変数を設定している

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

} 

}

答えて

2

:ここ

enter image description here

は、javaファイルですインスタンスメンバー:

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

したがってsetWordsQuestion、インスタンスメンバを使用している、あなたはnextWordsQuestionで設定した値が表示されません。

0

ランダムな値をinextに割り当てると、それをグローバル変数ではなくローカル変数に割り当てます。

修正するには、単にローカル変数を削除します。

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

バマー!私は私の目の中にほこりがあったに違いない! – narb

関連する問題