我正在構建使用保存的安卓遊戲。這意味着用戶最多可以在遊戲中打開5次獨特的保存。創建一個新的保存,即時通訊使用SharedPreferences讓用戶輸入一個保存名稱並將其保存在我的SharedPreferences文件夾中,然後用一個Intent Extra將用戶重定向到主遊戲活動,告訴遊戲活動什麼保存打開。Android應用程序崩潰(共享首選項)
在設計中它聽起來不錯,但由於某種原因我的應用程序只是保持部隊關閉。 我沒有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();
}
}
}
}
感謝您的幫助。
實際的比較字符串時,在申請截止?當你正在從列表中選擇一個保存的狀態? – 5hssba 2012-04-11 18:22:42
當應用程序關閉時發佈應用程序日誌 – 2012-04-11 18:28:14
然後,我100%確定您在按鈕clcick上的開始活動有任何問題 – 2012-04-11 20:48:04