2012-04-26 67 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); 
}