我的错误在哪里,因为我似乎无法找到它!
MainActivity:
package no.packname;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
// shared preferences
private SharedPreferences gamePrefs;
public static final String GAME_PREFS = "ArithmeticFile";
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void infob(View view) {
setContentView(R.layout.info);
}
public void score(View view) {
Intent highIntent = new Intent(this, HighScores.class);
this.startActivity(highIntent);
}
// method retrieves score
private int getScore() {
String scoreStr = Display.getText().toString();
return Integer
.parseInt(scoreStr.substring(scoreStr.lastIndexOf(" ") + 1));
}
// set high score
@SuppressLint("SimpleDateFormat")
private void setHighScore() {
int exScore = getScore();
if (exScore > 0) {
// we have a valid score
SharedPreferences.Editor scoreEdit = gamePrefs.edit();
DateFormat dateForm = new SimpleDateFormat("dd MMMM yyyy");
String dateOutput = dateForm.format(new Date());
// get existing scores
String scores = gamePrefs.getString("highScores", "");
// check for scores
if (scores.length() > 0) {
// we have existing scores
List<Score> scoreStrings = new ArrayList<Score>();
// split scores
String[] exScores = scores.split("\\|");
// add score object for each
for (String eSc : exScores) {
String[] parts = eSc.split(" - ");
scoreStrings.add(new Score(parts[0], Integer
.parseInt(parts[1])));
}
// new score
Score newScore = new Score(dateOutput, exScore);
scoreStrings.add(newScore);
// sort
Collections.sort(scoreStrings);
// get top ten
StringBuilder scoreBuild = new StringBuilder("");
for (int s = 0; s < scoreStrings.size(); s++) {
if (s >= 10)
break;
if (s > 0)
scoreBuild.append("|");
scoreBuild.append(scoreStrings.get(s).getScoreText());
}
// write to prefs
scoreEdit.putString("highScores", scoreBuild.toString());
scoreEdit.commit();
} else {
// no existing scores
scoreEdit.putString("highScores", "" + dateOutput + " - "
+ exScore);
scoreEdit.commit();
}
}
}
// set high score if activity destroyed
protected void onDestroy() {
setHighScore();
super.onDestroy();
}
// save instance state
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// save score and level
int exScore = getScore();
savedInstanceState.putInt("score", exScore);
// superclass method
super.onSaveInstanceState(savedInstanceState);
}
}
高分:
package no.packname;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
public class HighScores extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_high);
//get text view
TextView scoreView = (TextView)findViewById(R.id.high_scores_list);
//get shared prefs
SharedPreferences scorePrefs = getSharedPreferences(MainActivity.GAME_PREFS, 0);
//get scores
String[] savedScores = scorePrefs.getString("highScores", "").split("\\|");
//build string
StringBuilder scoreBuild = new StringBuilder("");
for(String score : savedScores){
scoreBuild.append(score+"\n");}
//display scores
scoreView.setText(scoreBuild.toString());
}
}`
分:
`public class Score implements Comparable<Score> {
//score date and number
private String scoreDate;
public int scoreNum;
public Score(String date, int num){
scoreDate=date;
scoreNum=num;
}
//check this score against another
public int compareTo(Score sc){
//return 0 if equal
//1 if passed greater than this
//-1 if this greater than passed
return sc.scoreNum>scoreNum? 1 : sc.scoreNum<scoreNum? -1 : 0;
}
//return score display text
public String getScoreText(){
return scoreDate+" - "+scoreNum;
}
Activity_High XML:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:padding="10dp"
tools:ignore="ContentDescription" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="@+id/backbutton"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/back"
android:onClick="bacm" />
<ImageView
android:id="@+id/operators"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/intro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/operators"
android:layout_centerHorizontal="true"
android:padding="20dp"
android:text="@string/app_name"
android:textColor="#ffffffff"
android:textSize="26sp"
android:textStyle="bold" />
<TextView
android:id="@+id/high_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/intro"
android:layout_centerHorizontal="true"
android:padding="20dp"
android:text="@string/hg"
android:textColor="#ffffffff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView android:id="@+id/high_scores_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/high_head"
android:layout_centerHorizontal="true"
android:gravity="center"
android:padding="10dp"
android:textColor="#ffffffff"
android:textSize="22sp"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
这是XML上的分数:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/infobutton"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="26sp" />