Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Eclipse Highscore Why i can don see the score?
    text
    copied!<p>Hi where is my error because I can't seem to find it!</p> <p>I hope any one can help me!</p> <p>MainActivity:</p> <pre><code>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 &gt; 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() &gt; 0) { // we have existing scores List&lt;Score&gt; scoreStrings = new ArrayList&lt;Score&gt;(); // 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 &lt; scoreStrings.size(); s++) { if (s &gt;= 10) break; if (s &gt; 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); } } </code></pre> <p>HighScore:</p> <pre><code>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()); } }` </code></pre> <p>Score:</p> <pre><code>`public class Score implements Comparable&lt;Score&gt; { //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&gt;scoreNum? 1 : sc.scoreNum&lt;scoreNum? -1 : 0; } //return score display text public String getScoreText(){ return scoreDate+" - "+scoreNum; } </code></pre> <p>Activity_High XML:</p> <pre><code> &lt; </code></pre> <p><strong>EDIT in response to:</strong> It looks like your post is mostly code; please add some more details:</p> <pre><code>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" &gt; &lt;RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" &gt; &lt;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" /&gt; &lt;ImageView android:id="@+id/operators" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:src="@drawable/ic_launcher" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; &lt;/ScrollView&gt; </code></pre> <p>And this is the Score on the XML:</p> <pre><code>&lt;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" /&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload