开发者

Passing a Resource to an Activity via an Intent

开发者 https://www.devze.com 2023-02-12 20:40 出处:网络
I have been trying for hours to pass a resource from one Activity to another via an Intent开发者_JAVA百科.

I have been trying for hours to pass a resource from one Activity to another via an Intent开发者_JAVA百科.

Here is the code from my 'Source' Activity:

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

            int res = R.raw.voicefile;

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

            startActivityForResult(myIntent, 0);

As you can see, I have a file called voicefile in my raw folder and I am setting res equal to it and passing it with my intent. (I am assuming it is of type int)

In my receiving Activity, I have:

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

At this point I was HOPING that file would equal R.raw.voicefile in my destination Activity and that I could use my variable 'file' in my MediaPlayer call as such:

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

versus:

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

My problem is whenever I click the button from my Source Activity that launches my Destination Activity, I get a Force Close.

Do you experts see anything glaringly wrong with my code?


try:

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

I think Intent.getExtras() assumes you have used a Bundle to map your extras in the Intent, which you have not.

0

精彩评论

暂无评论...
验证码 换一张
取 消