2012-04-26 69 views
1

你好我是這個網站的新手也有點新的android編程...android bundle不會繼續

每次我點擊按鈕去下一個活動,我得到一個力量關閉。我知道這個活動是有效的,因爲我評論了捆綁包..任何人都知道我做錯了什麼?

// click button on 1st activity 

    Intent iCreate = new 
Intent("silver.asw.charactersheet.CREATECHARACTER"); 
     iCreate.putExtra("cname",item); 
     startActivity(iCreate); 

// on item select 
item = spin.getItemAtPosition(position).toString(); 
// spinner is being populated by sql database 


// 2nd activity 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.character); 
    TextView character = (TextView)findViewById(R.id.tvViewCharacter); 

    Bundle b = this.getIntent().getExtras(); 
    String item = b.getString("cname"); 
    character.setText(item); 
} 

另外,我沒有任何警告或不能檢查我的logcat,因爲我使用的是一個Android應用程序ide的AIDE。 (之前,我離開了家,同樣的問題我已經測試此代碼在我的電腦上。)

+0

當你做到這一點'項目= spin.getItemAtPosition(位置)的ToString( );'? 'startActivity(iCreate)之前或之後;'? – candyleung 2012-04-26 03:06:27

+0

之後,在上面選擇的方法 – SilverWolfe 2012-04-26 03:31:29

+0

我不知道,看來你正在爲'String item = b.getString(「cname」);''獲取'null'。當'iCreate.putExtra(「cname」,item);''時你檢查過'item'的值嗎? – candyleung 2012-04-26 03:37:28

回答

0

你不投入任何意圖一束但在第二activity.use嘗試接受這種方式:

// 1nd activity 
item = spin.getItemAtPosition(position).toString(); 
Bundle bundle = new Bundle(); 
bundle.putString("cname", item); 
iCreate.putExtras(bundle); 

// 2nd activity 

Bundle bundle = this.getIntent().getExtras(); 
String name = bundle.getString("cname"); 
+0

位置是onItemSelected函數的一部分。我如何獲得選定的項目位置? – SilverWolfe 2012-04-26 03:27:39

+0

如果從onItemSelected存儲的值之外的某處開始活動,則讀取它it.try'public static String item =「」;' – 2012-04-26 03:32:07

+0

您是否在清單文件中輸入條目... – 2012-04-26 04:03:02

0

我不知道你的onClick函數在哪裏,但是試試這樣的例子。

初始化按鈕

Button b = (Button) findViewById(R.id.button1); 
    b.setOnClickListener(new OnClickListener() { 
     //start activity 
     public void onClick(View v) { 
      startActivity(new Intent(Main.this, StartPage.class)); 

     }} 
+0

onClick設置在意圖所在的位置..意圖是onclick方法內開關/外殼的一部分。 「item」var是所選方法的一部分 – SilverWolfe 2012-04-26 03:30:20

0
Bundle b = this.getIntent().getExtras(); 

下面替換上述線在第一活動

Bundle bundle = new Bundle(); 



Bundle b = getIntent().getExtras();//from 2 activity you can call as it no need to have this 
0

在第二活性,使用

String item = getIntent().getStringExtra("cname"); 

代替

Bundle b = this.getIntent().getExtras(); 
// b is null, because you use intent.putExtra(string, string). you should  
// use above method to get the data. 
String item = b.getString("cname"); 

這將導致NULLPointerException。

0

如果您使用此代碼

Intent iCreate = new Intent("silver.asw.charactersheet.CREATECHARACTER"); 
    iCreate.putExtra("cname",item); 
    startActivity(iCreate); 

在你的第二個活動,你可以使用這個樣子。

// 2nd activity 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.character); 
TextView character = (TextView)findViewById(R.id.tvViewCharacter); 

String item = getIntent().getExtras()..getString("cname"); 
character.setText(item); 
} 

或者你可以用這樣一種方式,

Intent iCreate = new Intent("silver.asw.charactersheet.CREATECHARACTER"); 
    Bundle b=new Bundle(); 
    b.putString("cname", item); 
    iCreate.putExtras(bundle); 
    startActivity(iCreate); 

在你的第二個活動

/ 2nd activity 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.character); 
TextView character = (TextView)findViewById(R.id.tvViewCharacter); 
Bundle b = getIntent().getExtras(); 
String item = b.getString("cname"); 
character.setText(item); 
}