我一直在尋找解決方案,以便在手機更改方向時如何讓我的應用不會丟失數據。我找到了一個,它將這行代碼添加到Android清單中。所以,我做到了:Android方向更改丟失數據解決方案無法正常工作
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我的數據還是迷路時方向的變化,任何人都可以幫我嗎?非常感謝你。
我的Java代碼:
public class MainActivity extends AppCompatActivity {
int scoreofShooter1 = 0;
int scoreofShooter2 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addOneForShooter1(View view) {
scoreofShooter1 = scoreofShooter1 + 1;
displayForShooter1(scoreofShooter1);
}
public void addTwoForShooter1(View view) {
scoreofShooter1 = scoreofShooter1 + 2;
displayForShooter1(scoreofShooter1);
}
public void addThreeForShooter1(View view) {
scoreofShooter1 = scoreofShooter1 + 3;
displayForShooter1(scoreofShooter1);
}
public void addFourForShooter1(View view) {
scoreofShooter1 = scoreofShooter1 + 4;
displayForShooter1(scoreofShooter1);
}
public void addOneForShooter2(View view) {
scoreofShooter2 = scoreofShooter2 + 1;
displayForShooter2(scoreofShooter2);
}
public void addTwoForShooter2(View view) {
scoreofShooter2 = scoreofShooter2 + 2;
displayForShooter2(scoreofShooter2);
}
public void addThreeForShooter2(View view) {
scoreofShooter2 = scoreofShooter2 + 3;
displayForShooter2(scoreofShooter2);
}
public void addFourForShooter2(View view) {
scoreofShooter2 = scoreofShooter2 + 4;
displayForShooter2(scoreofShooter2);
}
public void resetScoreOfShooter1(View view) {
scoreofShooter1 = 0;
displayForShooter1(scoreofShooter1);
}
public void resetScoreOfShooter2(View view) {
scoreofShooter2 = 0;
displayForShooter2(scoreofShooter2);
}
public void displayForShooter1(int score) {
TextView scoreView = (TextView) findViewById(R.id.shooter_1_score);
scoreView.setText(String.valueOf(score));
}
public void displayForShooter2(int score) {
TextView scoreView = (TextView) findViewById(R.id.shooter_2_score);
scoreView.setText(String.valueOf(score));
}
}
的[保存數據並改變方向]可能的複製(http://stackoverflow.com/questions/12214600/save-數據和變化的方向) –
嗨,對不起,它看起來像一個重複的,但我已經讀過這個話題,並不明白我的錯誤在哪裏,所以我會非常高興,如果有人幫助我的這個問題的具體情況,非常感謝你的理解。 – Veranicus