Search code examples
javaandroidandroid-mediaplayeronclicklistenertoast

Display Toast Within onClickListener


I'm currently experimenting with the MediaPlayer class in Android, and I'm currently trying to get the song duration to display as a toast, however I'm getting the following in my Stack Trace:

09-24 21:55:44.160 31729-31729/com.example.daf10.musicplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.daf10.musicplayer, PID: 31729
android.content.res.Resources$NotFoundException: String resource ID #0x4661f
    at android.content.res.Resources.getText(Resources.java:354)
    at android.widget.Toast.makeText(Toast.java:612)
    at com.example.daf10.musicplayer.MainActivity$3.onClick(MainActivity.java:46)
    at android.view.View.performClick(View.java:6921)
    at android.widget.TextView.performClick(TextView.java:12738)
    at android.view.View$PerformClick.run(View.java:26197)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:7002)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

Here is my Java Code from my MainActivity:

package com.example.daf10.musicplayer;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    private MediaPlayer mediaPlayer;
    private int duration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mediaPlayer = MediaPlayer.create(this, R.raw.song);


        Button playButton = findViewById(R.id.play_button);
        Button pauseButton = findViewById(R.id.pause_button);
        Button getSongDurationButton = findViewById(R.id.get_song_duration_button);

        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.start();
            }
        });

        pauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.pause();
            }
        });

        getSongDurationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                duration = mediaPlayer.getDuration();
                Toast.makeText(MainActivity.this,duration,Toast.LENGTH_LONG).show();
            }
        });



    }
}

And this is my XML Layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:orientation="vertical">

    <Button
        android:id="@+id/play_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play" />

    <Button
        android:id="@+id/pause_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pause" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/get_song_duration_button"
        android:text="Get Song Duration"/>

</LinearLayout>

The purpose of this app is just to get accustom to the MediaPlayer class.

If anyone can help point out where I'm going wrong, it would be tremendously appreciated :)

Thanks heaps! Matt.

Edit 25/09/2018 I noticed this question was marked as a duplicate and I'm guessing this was because of the similarities that the OP of the following link:Android, using Toast inside onClickListener

While he was trying to achieve the same thing by calling a toast within an OnClickListener, he was making a different mistake to me. It appears I'm using incorrect data types, whereas he getting "void cannot be converted to Toast" in his stack trace, I was getting fatal errors. So unfortunately I couldn't get my answer from that post. (Or my not so trained eyes couldn't figure it out :))


Solution

  • android.content.res.Resources$NotFoundException: String resource ID #0x4661f
    

    STRUCTURE

    Toast toast = Toast.makeText(context, text, duration).show();

    Looks like duration is int, You should convert to this String.

    • Convert using String.valueOf(int)

    Your Toast will be

    Toast.makeText(MainActivity.this,String.valueOf(duration),Toast.LENGTH_LONG).show();