简体   繁体   English

Android Studio:捆绑不起作用

[英]Android Studio: Bundle not working

In one of my activities I have a button, when pressed it stores a string value inside a bundle that I want to send to another activity and display in a TextView.在我的一个活动中,我有一个按钮,当按下它时,它会在一个包中存储一个字符串值,我想将其发送到另一个活动并显示在 TextView 中。

Code for when the bundle is created:创建包时的代码:

public void enemy_seen(View view){

    Intent send_enemy = new Intent(rear_gunner.this, pilot.class);
    String sight = "ENEMY SPOTTED";

    Bundle spotted = new Bundle();
    spotted.putString("TAG",sight);
    send_enemy.putExtras(spotted);



}

This code hapens on the button clicked and so far, from what I can tell this works....I believe.这段代码发生在点击按钮上,到目前为止,据我所知这是有效的......我相信。

When the bundle is called in second activity:在第二个活动中调用包时:

public class pilot extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pilot);
    //sets screen orientation on created
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    Bundle bundle = getIntent().getExtras();
    String something = bundle.getString("TAG");
    TextView enemy = (TextView) findViewById(R.id.enemy_spotted);
    enemy.setText(something);

}
}

The activity loads and crashes.活动加载并崩溃。 So it must be something to do with when using the bundle I believe?所以它一定与使用我相信的捆绑包有关吗?

在此处输入图片说明

I don't see you starting the activity from the intent you set the bundle.我没有看到您从设置捆绑包的意图开始活动。

The activity will only receive the bundle you put in an intent if you fire that activity with that intent.如果您使用该意图触发该活动,则该活动只会收到您放入该意图中的捆绑包。

You should do a startActivity(send_enemy) after setting the bundle to the intent.在将包设置为意图后,您应该执行startActivity(send_enemy)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM