2012-04-11 60 views
0

我正在構建使用保存的安卓遊戲。這意味着用戶最多可以在遊戲中打開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(); 
    } 
    } 
} 

} 

感謝您的幫助。

+0

實際的比較字符串時,在申請截止?當你正在從列表中選擇一個保存的狀態? – 5hssba 2012-04-11 18:22:42

+0

當應用程序關閉時發佈應用程序日誌 – 2012-04-11 18:28:14

+0

然後,我100%確定您在按鈕clcick上的開始活動有任何問題 – 2012-04-11 20:48:04

回答

1

到目前爲止,所有我看到的是一個很大的問題是這樣的:

if (clicked=="Empty save") 

==是數字和參考比較。你正在尋找:

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

將在問題

+0

修復,雖然它仍然無法正常工作..第二我單擊一個對象在列表中它向我顯示強制停止消息。你有什麼其他的建議? – arielschon12 2012-04-11 20:29:26

+0

@ arielschon12那麼...沒有任何理由可以調用super.onItemClick()我不知道這是否會拋出異常,但你可能不應該這樣做... – JRaymond 2012-04-11 20:34:21

+0

我認爲主要的東西那導致這是onClick對話框功能。在我用一些命令填充它之前,點擊其中一個菜單項將顯示對話框。它什麼都不會做。我可能還應該提到我之前沒有對SharedPreferences做過什麼,我只是使用了來自互聯網的命令。我需要設置它嗎? – arielschon12 2012-04-11 20:39:29