2014-06-20 101 views
-1

在我onPause我收集問題行和可見的按鈕數。然後在onResume中使用這些來恢復用戶所在的位置。底部的代碼是用於問題的,並且工作正常。問題在於可見的按鈕。 If語句被忽略。 Logcat告訴我,重新調整的可見按鈕(在這種情況下)的值是3,但它被忽略。我嘗試了不同的方法,例如在if語句之前將「vis」轉換爲整數纔會產生numberformat異常。這個當前的代碼是爲if語句的字符串聲明值,但無濟於事。我希望有人能指出我正確的方向。親切的問候德里克如果陳述被忽略

private void restoreVis() {  
    int i = 2; 
    int j = 3; 
    String strI = String.valueOf(i); 
    String strJ = String.valueOf(j);   
    String file1 = "vis"; 
    try { 
     BufferedReader inputReader = new BufferedReader(new InputStreamReader(
       openFileInput(file1))); 
     String inputString; 
     StringBuffer stringBuffer = new StringBuffer();     
     while ((inputString = inputReader.readLine()) != null) { 
      stringBuffer.append(inputString + "\n"); 
     } 
     String vis = stringBuffer.toString(); 
     Log.i(LOGTAG, vis + " visible buttons"); 

     if (vis.equals(strI)) { 
      eliminateChooser(); 
      eliminateChooser(); 
     Log.i(LOGTAG, "equals 2"); 
    } else if (vis.equals(strJ)) { 
      eliminateChooser(); 
      Log.i(LOGTAG, "equals 3"); 
    }else{ 
     Log.i(LOGTAG, "vis equals 4"); 
    } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    Log.i(LOGTAG," end of vis");    
} 


    LogCat 
    06-20 17:50:07.603: I/ON(18818): 3 visible buttons 
     06-20 17:50:07.608: I/ON(18818): vis equals 4 
     06-20 17:50:35.513: I/ON(18818): on Pause called 
     06-20 17:50:35.513: I/ON(18818): 8 row Id 
     06-20 17:50:35.513: I/System.out(18818): Visible children: 4 

     private void restoreGameState() { 
    String file = "row"; 
    try { 
     BufferedReader inputReader = new BufferedReader(new InputStreamReader(
       openFileInput(file))); 
     String inputString; 
     StringBuffer stringBuffer = new StringBuffer();     
     while ((inputString = inputReader.readLine()) != null) { 
      stringBuffer.append(inputString + "\n"); 
     } 
     String row = stringBuffer.toString(); 
     quest = datasource.findGameState(null, row); 
     refreshDisplay(); 
     Log.i(LOGTAG, row + " value of rowId"); 


    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 
+0

您的file1似乎不正確。我認爲它必須像「vis.txt」,沒有文件結尾 – Opiatefuchs

回答

4

我覺得這種行爲背後的原因是片斷是您比較3\n3這是不相等的。從stringBuffer.append(inputString + "\n");刪除\n

+0

非常感謝你,我的問題現在解決了。 \ n沒有在logcat或文件中顯示,所以我沒有意識到它正在追加它。 – nybler42

+0

這發生在我之前;) – hoomi

0

我建議你使用這樣的:

else if (Integer.valueOf(vis).intValue() == j) 

代替。整數比較更容易。

+0

嗨感謝您的迴應,我按照你說的做了,但想出了java.lang.NumberFormatException:無效的int。我曾嘗試類似的東西,但有同樣的錯誤。親切的問候,德里克 – nybler42