我需要使這個變量保存並做到這一點,我將它導出到.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();
}
});}
什麼錯誤信息是在捕獲異常? –
將'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
「12-20 17:57:23.720 2297-2297/com.coastercounter.nyphoria.coastercounter E/Exception:java.io.FileNotFoundException:AltonAirCount.txt:打開失敗:EROFS(只讀文件系統)」我是猜測我的模擬器是隻讀的 –