Android应用崩溃(共享首选项)

时间:2012-04-11 18:15:58

标签: android sharedpreferences alertdialog

我正在构建一个使用保存的Android游戏。这意味着用户可以在游戏中打开最多5个独特的保存。创建新的保存,即时通讯使用SharedPreferences让用户输入保存名称并将其存储在我的SharedPreferences文件夹中,然后将用户重定向到主游戏Activity,其中包含Intent Extra,告诉游戏活动保存打开的内容。 / p>

在设计中一切听起来都不错,但出于某种原因我的应用程序只是让Force关闭。 我在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();
    }
    }
}

}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

到目前为止,我认为这是一个很大的问题:

if (clicked=="Empty save")

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

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

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