0

我正在构建一个使用保存的 android 游戏。这意味着用户最多可以在游戏中打开 5 个独特的存档。要创建新的存档,我使用 SharedPreferences 让用户输入存档名称并将其存储在我的 SharedPreferences 文件夹中,然后使用 Intent Extra 将用户重定向到主游戏 Activity,该 Intent Extra 告诉游戏活动要打开什么存档。

在设计中听起来一切正常,但由于某种原因,我的应用程序只是保持强制关闭。我在 Eclipse 中没有任何代码错误,甚至 LogCat 也没有显示错误。我不知道发生了什么...

这是我的代码:

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();
    }
    }
}

}

谢谢你的帮助。

4

1 回答 1

1

到目前为止,我所看到的一个大问题是:

if (clicked=="Empty save")

==用于数字和参考比较。您正在寻找:

if (clicked.equals("Empty save")) 

这实际上会比较有问题的字符串

于 2012-04-11T18:36:54.873 回答