0

There is an annoying bug in my code somewhere and I can't sleep because of it!

I kind of get why I might be getting it - I think something to do with onCreate method I think.

This is what I have:

public class PredefinedUserPlayer extends Activity {

    MediaPlayer mediaPlayer;
    String names;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userdefplayer);
        names = "http://X.X.X.X/musikz/musikz01.mp3";
        mediaPlayer = new MediaPlayer();
    }

    public void playSelectedFile() {
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(PredefinedUserPlayer.this, Uri.parse(names));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.start();
    }
}

The playSelectedFile() method is being called in another class and here is that bit of code here:

  view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PredefinedUserPlayer predefinedUserPlayer = new PredefinedUserPlayer()
                    predefinedUserPlayer.playSelectedFile();
                }
            });

I can't really have the MediaPlayer stuff in the above class which is why I have had to instantiate the PredefinedUserPlayer class. This is the actual stack trace (maybe someone can help me debug and fix this annoying bug):

 java.lang.NullPointerException
            at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:99)
            at android.media.MediaPlayer.setDataSource(MediaPlayer.java:882)
            at android.media.MediaPlayer.setDataSource(MediaPlayer.java:859)
            at lukasz.musik.PredefinedUserPlayer.playSelectedFile(PredefinedUserPlayer.java:86)
            at lukasz.musik.CustomMusicAdapter$ViewHolder$1.onClick(CustomMusicAdapter.java:85)
            at android.view.View.performClick(View.java:4240)
            at android.view.View$PerformClick.run(View.java:17721)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
  • Wonderful, the person who closed this marked it as a dup of another issue marked as a dup, which does not answer the question. – Doug Stevenson Feb 24 '16 at 01:57
  • Exactly! I had to repost as I never really got an answer for the last post. @EJP it would actually help if you stop marking my posts as `duplicate`. They are NOT duplicates and if you can't answer the question then dont touch my OP please! – Lukazs Pioetrszci Feb 24 '16 at 02:01
  • I tested that code. Replace it with playSelectFile() method. public void playSelectedFile() { mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); Thread thread = new Thread() { @Override public void run() { try { mediaPlayer.setDataSource("http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3"); } catch (IOException e) { e.printStackTrace(); } – Usman Feb 24 '16 at 04:09
  • half code. try { mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.start(); } }; thread.start(); } and dn't forget to take internet permission. – Usman Feb 24 '16 at 04:09
  • Hi I still keep getting the `nullpointerexception` error. It keeps complaining about `setDataSource` – Lukazs Pioetrszci Feb 24 '16 at 10:26

1 Answers1

0

You can't new activity by hand like this:

PredefinedUserPlayer predefinedUserPlayer = new PredefinedUserPlayer()

You can use Broadcast to achieve it,Like this: **First,Register a BroadcastReceiver in PredefinedUserPlayer Activity:

public static String ACTION_PLAY = "ACTION_PLAY_FILE";
private final BroadcastReceiver mPlayFileReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (ACTION_PLAY.equals(action)) {
              playSelectedFile();
        }
    }
};
IntentFilter intentFilter = new IntentFilter(ACTION_PLAY);

LocalBroadcastManager.getInstance(this).registerReceiver(mPlayFileReceiver,intentFilter);

Second,Send Broadcast with action ACTION_PLAY to inform the receiver to be invoked:

LocalBroadcastReceiver.getInstance(this).sendBroadcastSync(new Intent(PredefinedUserPlayer.ACTION_PLAY));
starkshang
  • 8,228
  • 6
  • 41
  • 52
  • I had to because the `onClickListener` was in another class – Lukazs Pioetrszci Feb 24 '16 at 01:56
  • @LukazsPioetrszci,you can use broadcastreceiver to make it. – starkshang Feb 24 '16 at 02:02
  • Please explain or apply it to my context - it would really mean a lot – Lukazs Pioetrszci Feb 24 '16 at 02:04
  • @LukazsPioetrszci,I mean you create a BroadcastReceiver in `PredefinedUserPlayer ` with specified action,for exampe "ACTION_PLAY_FILE" and implement its `onReceive` method to call `playSelectedFile `,when you want to call this method in another activity,send a Broadcast with the action "ACTION_PLAY_FILE",then the method will be invoked and you don't need to new an activity.And please remember,don't new an activity by hand whenever. – starkshang Feb 24 '16 at 09:38
  • Could you please edit your answer and write it for me if you don't mind. I still don't understand what to do – Lukazs Pioetrszci Feb 24 '16 at 09:54
  • I HAVE FIXED THIS BUG - I CANT BELIEVE I SPENT LIKE 3 HOURS STARING AT IT, IT WAS THE `mediaPlayer.setDataSource()`. You can just pass the URL as a string. The error was something else the whole time – Lukazs Pioetrszci Feb 24 '16 at 10:46
  • congratulations for found the problem,But I also suggest you use broadcast way to invoke another activity's method,this is be more better. – starkshang Feb 27 '16 at 02:49