【发布时间】: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 也会导致错误