【问题标题】:Passing a Resource to an Activity via an Intent通过 Intent 将资源传递给 Activity
【发布时间】:2011-02-24 00:29:18
【问题描述】:

我已经尝试了几个小时通过 Intent 将资源从一个 Activity 传递到另一个。

这是来自我的“源”活动的代码:

           Intent myIntent = new Intent(view.getContext(), Activity3.class);

            int res = R.raw.voicefile;

           myIntent = myIntent.putExtra("soundfile", res);

            startActivityForResult(myIntent, 0);

如您所见,我的原始文件夹中有一个名为 voicefile 的文件,我将 res 设置为等于它并按照我的意图传递它。 (我假设它是 int 类型)

在我的接收活动中,我有:

    Intent sender=getIntent();
    int file=sender.getExtras().getInt("soundfile");

此时我希望该文件在我的目标 Activity 中等于 R.raw.voicefile,并且我可以在我的 MediaPlayer 调用中使用我的变量“文件”:

          MediaPlayer.create(getBaseContext(), file).start();

对比:

          MediaPlayer.create(getBaseContext(), R.raw.voicefile).start();

我的问题是,每当我从源活动中单击启动目标活动的按钮时,我都会收到强制关闭。

各位专家认为我的代码有什么明显的问题吗?

【问题讨论】:

  • 错误是什么?日志应该有详细的信息。

标签: android


【解决方案1】:

尝试:

int file=sender.getIntExtra("soundfile", 0);

我认为Intent.getExtras() 假设您已经使用Bundle 将您的额外内容映射到Intent,而您还没有。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    相关资源
    最近更新 更多