0

IM GETTING THIS ERROR ON THIS LINE Button button1 = (Button) findViewById(R.id.alarm);

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.user.appidea/com.example.user.appidea.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

   public class MainActivity extends AppCompatActivity {
    private Button button;
    **Button button1 = (Button) findViewById(R.id.alarm);**


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

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                INSERT CODE HERE

            }
        });
    }
}
Coder32
  • 29
  • 1
  • 4
  • Possible duplicate of [Play sound on button click android](https://stackoverflow.com/questions/18459122/play-sound-on-button-click-android) – Lex Lustor Dec 27 '18 at 10:57
  • 2
    Possible duplicate of [How to play ringtone/alarm sound in Android](https://stackoverflow.com/questions/2618182/how-to-play-ringtone-alarm-sound-in-android) – Android Dec 27 '18 at 10:58
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Matt Ke Dec 27 '18 at 11:55

1 Answers1

0

try this working example

final MediaPlayer mpRingtone = MediaPlayer.create(this, R.raw.musicfile);
mpRingtone.start();
mitesh makwana
  • 259
  • 3
  • 11
  • how do i get the musicfile in my project; when I put it in my raw folder it thinks it is a text file – Coder32 Dec 27 '18 at 11:26
  • musicfile is your ringtone file.you have to paste your ringtone in raw folder – mitesh makwana Dec 27 '18 at 11:27
  • IM GETTING THIS ERROR ON THIS LINE Button button1 = (Button) findViewById(R.id.alarm); java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.user.appidea/com.example.user.appidea.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference – Coder32 Dec 27 '18 at 11:40
  • check your button id is available or not in your XML – mitesh makwana Dec 27 '18 at 11:48
  • declare button1 in onCreate – mitesh makwana Dec 27 '18 at 11:52