我从第一个意图中打印出正确的id,但没有正确发送给第二个意图。
第一个意图的项目点击:(对于以后的观众,这个问题已经更新了正确的答案):
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
Intent intent = new Intent(videoChannelActivity.this,
PlayVideoActivity.class);
intent.putExtra("videoId", l);
System.out.println("video id intent1 = " + l);
startActivity(intent);
}
});
log: 05-08 17:40:40.505: I/System.out(13938): video id intent 1: 2
第二意图创建:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final VideoView videoView;
setContentView(R.layout.activity_play_video);
Bundle extras;
long videoLong;
if (savedInstanceState == null) {
extras = getIntent().getExtras();
if (extras == null) {
videoLong = 0;
} else {
videoLong = extras.getLong("videoId");
System.out.println("video id intent 2 = " + videoLong);
}
}
log: 05-08 17:46:19.166: I/System.out(14382): video id intent 2 = null
l 是类型的长…
所以你需要提取long而不是像
那样的String videoLong = extras.getLong("videoId");
所以第二个意图的完整实现是
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final VideoView videoView;
setContentView(R.layout.activity_play_video);
Bundle extras;
Long videoLong;
if (savedInstanceState == null) {
extras = getIntent().getExtras();
if (extras == null) {
videoLong= null;
} else {
videoLong= extras.getLong("videoId");
System.out.println("video id intent 2 = " + videoLong);
}
}
您正在传递long
作为额外的,但试图将其读取为String.