-2

My code is

package add; 

public class Addfunction {

MediaPlayer _mediaPlayer;
Context context;
public   void  playFromResource (int resId)  {
  if (_mediaPlayer != null) {
    _mediaPlayer.reset(); 
    }
    _mediaPlayer = MediaPlayer.create(context, resId);
    _mediaPlayer.start();
    _mediaPlayer.setLooping(true);                                                      
    new Handler().postDelayed(new Runnable() 
    { 
        @Override public void run() 
        { 
            if(_mediaPlayer!=null && _mediaPlayer.isLooping()) 
                _mediaPlayer.stop(); 
        }
    }, 60000);

    }
}       

Next package contains

package com.example.demoo;

import add.Addfunction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {


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

        Addfunction ring=new Addfunction();

        ring.playFromResource(R.raw.beep);
    }

while running the program it shows"Unfortunately application is stop". Log cat shows a Null pointer Exception Please help me to identify my error.

samgak
  • 22,290
  • 4
  • 50
  • 73
  • 1
    Post logcat. And format code, please – MysticMagicϡ Jan 23 '15 at 05:34
  • 2
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Vivek Jan 23 '15 at 05:36

1 Answers1

1

Your context is null ,pass the context with your method

ring.playFromResource(R.raw.beep,this);

and assign it to your context in Addfunction class.

Tarun Varshney
  • 16,808
  • 6
  • 32
  • 47