其他活动使用的bundle中未显示的字符串



我想把披萨的名字放在账单页面上,但它没有显示(空白(。

菜单.java:

bundle = new Bundle();
        bundle.putString("name",pizzaName);
y = new Intent(this,Bill.class);
        y.putExtras(bundle);
startActivity(y);

账单.java:

text1=(TextView)findViewById(R.id.textView6);
y= new Intent();
        bundle=getIntent().getExtras();
        Name=bundle.getString("name");
text1.setText();

输出:

您不需要在Bill.java 中执行y= new Intent();

按如下方式更改Bill.java中的代码。此外,为了更好地命名约定,请将Bill.java更改为BillActivity.java

    TextView text1 = (TextView) findViewById(R.id.textView6);
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        String name = bundle.getString("name");
        text1.setText(name);
    }

您可以像这样简单地传递和检索值。

// Creating and initializing an Intent object
Intent intent = new Intent(this, NextActivity.class);
// Attach the key value pair using putExtra to this intent
String pizza = "Margherita";
intent.putExtra("PIZZA_NAME", pizza);
// Start the activity
startActivity(intent);
// Get the current intent
Intent intent = getIntent();
// Get the attached extras from the intent
// We should use the same key as we used to attach the data.
String pizza = intent.getStringExtra("PIZZA_NAME");

Remove y=new Intent((;在您的账单中。类别

无需获取y=new Intent((;当您从捆绑中获取数据时

text1=(TextView(findViewById(R.id.textView6(;

bundle=getIntent((.getExtras((;

Name=bundle.getString("Name"(;

text1.setText((;

最新更新