【问题标题】:Android Studio: Bundle not workingAndroid Studio:捆绑不起作用
【发布时间】:2017-02-14 13:04:07
【问题描述】:

在我的一个活动中,我有一个按钮,当按下它时,它会在我想要发送到另一个活动并显示在 TextView 中的包中存储一个字符串值。

创建包时的代码:

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



}

此代码发生在单击的按钮上,到目前为止,据我所知这是有效的......我相信。

在第二个活动中调用捆绑包时:

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

}
}

活动加载并崩溃。所以我相信在使用bundle时一定是有什么关系?

【问题讨论】:

  • 发布崩溃日志
  • 您可以检查 null 的 intent , Bundle 并检查 Bundle 是否包含“TAG”,然后您会找到崩溃的实际原因。并调试您的代码或打印日志,以便清楚问题所在。
  • 你发布崩溃日志,textview null reference cal 也会导致错误

标签: java android bundle


【解决方案1】:

我没有看到您从设置捆绑包的意图开始活动。

只有当您使用该意图触发该活动时,该活动才会收到您放入该意图的捆绑包。

在将捆绑包设置为 Intent 之后,您应该执行 startActivity(send_enemy)

【讨论】:

  • @GeorgeBrooks 这是否解决了您的问题?如果是,请采纳答案
猜你喜欢
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
相关资源
最近更新 更多