1

你好,我是这个网站的新手,对安卓编程也有点陌生......

每次我单击按钮进入下一个活动时,我都会强制关闭。我知道该活动有效,因为我注释掉了捆绑包..有人知道我做错了什么吗?

// 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,因为我正在使用 AIDE,它是一个 android 应用程序 ide。(我离开家之前已经在我的电脑上测试过这段代码,同样的问题。)

4

5 回答 5

0

我不确定您的 onClick 功能在哪里,但请尝试类似 Ex.

初始化按钮

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));

        }}
于 2012-04-26T03:08:04.217 回答
0

you are not putting any bundle in intent but trying receiving in second activity.use this way:

// 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");
于 2012-04-26T03:06:15.503 回答
0
 Bundle b = this.getIntent().getExtras();

replace the above line with below in first activity

Bundle bundle = new Bundle();



Bundle b = getIntent().getExtras();//from 2 activity you can call as it no need to have this
于 2012-04-26T03:16:29.173 回答
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。

于 2012-04-26T03:28:27.623 回答
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);
}
于 2012-04-26T05:03:36.127 回答