I am building an android game that uses saves. That means that a user can open up to 5 unique saves on the game. to create a new save, im using SharedPreferences to let the user enter a save name and store it in my SharedPreferences folder, and then redirect the user to the main game Activity with an Intent Extra that tells the game activity what save to open.
In the design it all sounds ok, but for some reason my application just keeps Force closing.
I dont have any code errors inside eclipse, and even LogCat shows no error. I dont know what
is going on…
Here is my code:
package ent.com;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class Saves extends ListActivity{
String saves[] = { "Empty save", "Empty save", "Empty save", "Empty save", "Empty save"};
public static String filename = "SharedData";
SharedPreferences someData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
someData = getSharedPreferences(filename, 0);
String save1 = someData.getString("0", "Empty save");
String save2 = someData.getString("1", "Empty save");
String save3 = someData.getString("2", "Empty save");
String save4 = someData.getString("3", "Empty save");
String save5 = someData.getString("4", "Empty save");
saves[0] = save1;
saves[1] = save2;
saves[2] = save3;
saves[3] = save4;
saves[4] = save5;
setListAdapter(new ArrayAdapter<String>(Saves.this, android.R.layout.simple_list_item_1, saves));
}
@Override
protected void onListItemClick(ListView l, View v,int position, long id) {
final String clicked = saves[position];
final String pos = getString(position);
super.onListItemClick(l, v, position, id);
if(clicked=="Empty save"){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Input name");
alert.setMessage("Give your team a name:");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
SharedPreferences.Editor editor = someData.edit();
editor.putString(pos, value);
editor.commit();
try{
Class Joe = Class.forName("ent.com.TestActivity");
Intent Joey = new Intent(Saves.this, Joe);
Joey.putExtra("save", clicked);
startActivity(Joey);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent back = new Intent("ent.com.MENU");
startActivity(back);
}
});
alert.show();
}else{
try{
Class ourClass = Class.forName("ent.com.TestActivity");
Intent ourIntent = new Intent(Saves.this, ourClass);
ourIntent.putExtra("save", clicked);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
}
Thanks for any help.
So far all I see that is a big problem is this :
==is for numbers and reference comparison. you’re looking for:which will actually compare the strings in question