2015-12-20 63 views
-2

我需要使這個變量保存並做到這一點,我將它導出到.txt文件。但是,它並沒有這樣做,它被抓住了。有人可以告訴我我做錯了什麼。提前致謝。無法將變量放入一個txt文件

public class AltonAir extends AppCompatActivity { 

public class AltonAir extends AppCompatActivity { 
SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("AltonAirCount", Context.MODE_PRIVATE); 

public int rideCount = sharedPref.getInt("RideCount", 0); 

void updateListString() { 

    final int[] Track = {840}; 
    int localTrack = Track[0]; 
    final int[] Inversions = {2}; 
    int localInversions = Inversions[0]; 
    final int[] Time = {100}; 
    int localTime = Time[0]; 
    final double[] gForce = {3.5}; 
    double localGForce = gForce[0]; 

    final String[][] airStatsString = {{"Length Covered: " + localTrack * rideCount + "m", 
      "Total Inversions Experienced: " + localInversions * rideCount, 
      "Total Time on Air: " + localTime * rideCount + "s", 
      "Total G-Force Experienced:" + localGForce * rideCount + "G's"}}; 

    ListAdapter theAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
      airStatsString[0]); 

    ListView theListView = (ListView) findViewById(R.id.airStats); 

    theListView.setAdapter(theAdapter); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_alton_air); 

    final File file = new File("AltonAirCount.txt"); 

    int rideCountFile; 





    onCreate2(file, sharedPref);} 

void onCreate2(final File file, final SharedPreferences sharedPref){ 


    TextView triviaText = (TextView) findViewById(R.id.airTriviaText); 



    String rideCountString = Objects.toString(rideCount); 

    final TextView[] totalRideText = {(TextView) findViewById(R.id.totalRide)}; 
    totalRideText[0].setText("Total Flights : " + rideCountString); 

    Random rand = new Random(); 
    final int[] triviaNumber = {rand.nextInt(4) + 1}; 

    if (triviaNumber[0] == 1) { 
     triviaText.setText("Air actually stands for Aerial Inversion Ride"); 
    } else if (triviaNumber[0] == 2) { 
     triviaText.setText("Air initially went over budget so there wasn’t much theming"); 
    } else if (triviaNumber[0] == 3) { 
     triviaText.setText("Air is one of the only rides at Alton Towers you can see above the tree line"); 
    } else if (triviaNumber[0] == 4) { 
     triviaText.setText("Air is the latest ride to be manufactured by B&M at the park."); 
    } else { 
     triviaText.setText("mmm"); 
    } 

    updateListString(); 


    Button triviaPlus = (Button) findViewById(R.id.triviaPlus); 

    triviaPlus.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      triviaNumber[0] += 1; 

      if (triviaNumber[0] == 5) { 
       triviaNumber[0] = 1; 
      } 

      TextView triviaText = (TextView) findViewById(R.id.airTriviaText); 
      if (triviaNumber[0] == 1) { 
       triviaText.setText("Air actually stands for Aerial Inversion Ride"); 
      } else if (triviaNumber[0] == 2) { 
       triviaText.setText("Air initially went over budget so there wasn’t much theming"); 
      } else if (triviaNumber[0] == 3) { 
       triviaText.setText(" Air is one of the only rides at Alton Towers you can see above the tree line"); 
      } else if (triviaNumber[0] == 4) { 
       triviaText.setText(" Air is the latest ride to be manufactured by B&M at the park."); 
      } else { 
       triviaNumber[0] = 1; 
      } 


     } 
    }); 

    Button triviaMinus = (Button) findViewById(R.id.trivaMinus); 

    triviaMinus.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      triviaNumber[0] -= 1; 

      TextView triviaText = (TextView) findViewById(R.id.airTriviaText); 
      if (triviaNumber[0] == 0) { 
       triviaNumber[0] = 4; 
       triviaText.setText(" Air is the latest ride to be manufactured by B&M at the park."); 
      } else if (triviaNumber[0] == 1) { 
       triviaText.setText("Air actually stands for Aerial Inversion Ride"); 
      } else if (triviaNumber[0] == 2) { 
       triviaText.setText("Air initially went over budget so there wasn’t much theming"); 
      } else if (triviaNumber[0] == 3) { 
       triviaText.setText(" Air is one of the only rides at Alton Towers you can see above the tree line"); 
      } else if (triviaNumber[0] == 4) { 
       triviaText.setText(" Air is the latest ride to be manufactured by B&M at the park."); 
      } else { 
       triviaNumber[0] = 4; 
      } 


     } 
    }); 

    Button ridePlus = (Button) findViewById(R.id.ridePlus); 

    ridePlus.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      rideCount += 1; 
      String rideCountString = Objects.toString(rideCount); 

      TextView totalRideText = (TextView) findViewById(R.id.totalRide); 
      totalRideText.setText("Total Flights : " + rideCountString); 

      updateListString(); 

      try { 
       PrintWriter output = new PrintWriter(file); 
       output.println(rideCount); 
       output.close(); 

      } catch (IOException ex) { 

       Log.e("Exception ", String.valueOf(ex)); 


       Toast toast = Toast.makeText(getApplicationContext(), "It's been caught", Toast.LENGTH_SHORT); 

       toast.show(); 
      } 

     } 
    }); 

    Button rideMinus = (Button) findViewById(R.id.rideMinus); 

    rideMinus.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      rideCount -= 1; 
      if (rideCount < 0) { 
       rideCount = 0; 
       Toast toast = Toast.makeText(getApplicationContext(), "Ride Count can't go below zero.", Toast.LENGTH_SHORT); 

       toast.show(); 
      } 
      String rideCountString = Objects.toString(rideCount); 

      TextView totalRideText = (TextView) findViewById(R.id.totalRide); 
      totalRideText.setText("Total Flights : " + rideCountString); 


      updateListString(); 

      SharedPreferences.Editor editor = sharedPref.edit(); 
      editor.putInt("RideCount", (int) rideCount); 
      editor.commit(); 
     } 
    });} 
+3

什麼錯誤信息是在捕獲異常? –

+3

將'Log.e(「Zak」,「Exception write to file」,ex);'添加到'catch'子句中,再次運行該應用程序,然後使用LogCat檢查Java堆棧跟蹤:https:// stackoverflow。 com/questions/23353173 /不幸的是,myapp-has-stopped-how-can-i-solve-this **永遠不會創建一個異常處理程序,它不會將異常記錄在某處,您可以看到它。 – CommonsWare

+0

「12-20 17:57:23.720 2297-2297/com.coastercounter.nyphoria.coastercounter E/Exception:java.io.FileNotFoundException:AltonAirCount.txt:打開失敗:EROFS(只讀文件系統)」我是猜測我的模擬器是隻讀的 –

回答

1

也許你應該考慮寫入SharedPreferences而不是本地文件:(警告一些假設你的方法,如getApplicationContext()已經到位,該字符串應該從資源,而不是使用文本採取)

SharedPreferences sharedPref = getApplicationContext() 
     .getSharedPreferences("AltonAirCount", Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedPref.edit(); 
editor.putInt("RideCount", rideCount); 
editor.commit(); 

讀值回:

//         Key   Default Value    
long rideCount = sharedPref.getInt("RideCount", 0); 

關於你的「最後」的問題

編輯您通過void onCreate2(final long rideCount,可能是因爲你的IDE建議 - 否則你將無法看到rideCount在內部類像rideplus OnClickListener。

但是通過實現可視性,您失去了分配新值的能力。

我建議你讓rideCount成爲你班級的成員變量。

+0

感謝您的回覆。我將如何將這個號碼退出?我假設它與我已得到的不同 –

+0

在答案中查看我的編輯。 – Jan

+0

謝謝。我已經添加了該變量,但在計算中使用該變量的部分表示「無法爲最終變量rideCount賦值」。爲什麼這可能是 –