2012-01-12 53 views
1

好吧,我有我的應用程序,迄今爲止從SplashScreen中打開一個新的Activity。一旦這個活動打開,有一個按鈕添加球員,它打開了一個新的Activty。在這Activty我有一個Array這將填充一個EditText框。傳遞陣列到新的活動

一旦玩家被添加,播放按鈕將被點擊,這將回到以前的Activity

我的問題是當播放按鈕被點擊時,你回到以前的Activty我需要Array結轉和填充String

代碼更新 - 現在沒有任何力量接近,但dosent陣列似乎超過了承載和填充strArray

我有這個按鈕承載Array一旦球員已添加:

Button play = (Button) findViewById(R.id.playnow); 
play.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
Intent i = new Intent(demo.AddRemove.this, demo.play.class); 
Bundle extras = new Bundle(); 
extras.putStringArrayList("KEY", playerList); 
i.putExtras(extras); 
startActivity(i); 

}}); 
} 

並且這顯示Array被結轉:

super.onCreate(savedInstanceState); 
    Intent intent = getIntent(); 
    if (extras == null) { 
     strArray = new String[0]; 
    } else { 
     ArrayList<String> myStrings = extras.getStringArrayList("KEY"); 
     if (myStrings == null) { 
      strArray = new String[0]; 
     } else { 
      strArray = (String[]) myStrings.toArray(); 
     } 
    } 

    setContentView(R.layout.passw_layout); 

    initWheel(R.id.passw_1, strArray); 

但正如我之前所說當我嘗試從SplashScreen打開第一個Activty play.java時,它將以java.lang.NullPointerException關閉。我認爲這是因爲extras是空的。

+0

請張貼此代碼initWheel(R.id.passw_1,strArray); – sfratini 2012-01-12 15:33:25

+0

@sfratini對不起,哪位? – Matt 2012-01-12 15:34:36

+0

我不確定extras是否與getIntent()。getExtras()相同。嘗試用該替換電話。我的意思是方法,所以檢查你的問題是否存在。 – sfratini 2012-01-12 15:36:41

回答

2

試試這個:

String[] strArray; 
Intent intent = getIntent(); 
Bundle extras = intent.getExtras(); //Added this 
if (extras == null) { 
    strArray = new String[0]; 
} else { 
    ArrayList<String> myStrings = extras.getStringArrayList("KEY"); 
    if (myStrings == null) { 
     strArray = new String[0]; 
    } else { 
     strArray = (String[]) myStrings.toArray(); 
    } 
} 
+0

這導致了一個關閉的力量。我只是給你發了個邀請:D – Matt 2012-01-12 15:43:04

+0

太好了。沒問題。我會加入片刻 – sfratini 2012-01-12 16:06:04

+0

http://chat.stackoverflow.com/rooms/6658/matt-array – Matt 2012-01-12 16:12:25

1

添加一個簡單的檢查爲空,並創建一個空數組:

String[] strArray; 
if (extras == null) { 
    strArray = new String[0]; 
} else { 
    ArrayList<String> myStrings = extras.getStringArrayList("KEY"); 
    if (myStrings == null) { 
     strArray = new String[0]; 
    } else { 
     strArray = (String[]) myStrings.toArray(); 
    } 
} 

如果你不喜歡這樣的深度嵌套的if語句,你也可以創建一個方法來提取陣列,和使用盡早返回返回數組,只要找到空值。

+0

我明白你做了什麼,但沒有在哪裏實施它? – Matt 2012-01-12 15:20:32

+0

不用介意,但出於某種原因,它不會攜帶數組並顯示它?你可以加入我的聊天嗎? – Matt 2012-01-12 15:25:04