androidforceclosesoundeffect

Application suddenly Force stop when clicking button with Sound


I create a project, and this is MainActivity class

package com.example.myproject;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.*;

public class MainActivity extends Activity {

Button btnmulai, btnpengaturan, btnexit,btninfo;
MediaPlayer player;

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

    btnpengaturan=(Button)findViewById(R.id.btnPengaturan);
    btnmulai=(Button)findViewById(R.id.btnMulai);
    btnexit=(Button)findViewById(R.id.btnExit);
    btninfo=(Button)findViewById(R.id.btnInfo);

    btnpengaturan.setOnClickListener(new View.OnClickListener()
    {   
        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainActivity.this, Pengaturan.class);
            playSound(1);
            startActivity(intent);
        }   
    });

    btnmulai.setOnClickListener(new View.OnClickListener()
    {   
        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainActivity.this, Prolog.class);
            playSound(1);
            startActivity(intent);          
        }
    });

    btninfo.setOnClickListener(new View.OnClickListener()
    {   
        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            playSound(1);           
            AlertDialog.Builder alertinfo  = new AlertDialog.Builder(MainActivity.this);

            alertinfo.setMessage("Info saja");
            alertinfo.setTitle("INFO");
            alertinfo.setPositiveButton("OK", null);
            alertinfo.setCancelable(true);
            alertinfo.create().show();
        }
    });


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

            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    switch (which)
                    {
                        case DialogInterface.BUTTON_POSITIVE:
                        // TODO Auto-generated method stub
                        finish();
                        System.exit(0);
                        break;

                        case DialogInterface.BUTTON_NEGATIVE:
                        //No button clicked
                        break;
                    }
                }
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setMessage("Anda yakin ingin keluar?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();   
        }
    });
}


public void playSound(int arg)
{
    try
    {
        if (player.isPlaying()) 
        {
            player.stop();
            player.release();
        }
    }
    catch(Exception e)
    {

    }

    if (arg == 1)
    {
        player = MediaPlayer.create(this, R.raw.atur);
    }
    player.setLooping(false);
    player.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

When i try to compile on Bluestacks or Galaxy S4, it is working, but when i tried to compile on a Tablet, and i press the btnmulai or btninfo, it said that "Unfortunately, myproject has stopped".. This is the Logcat error:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.myproject.MainActivity.playSound(MainActivity.java:135)
at com.example.myproject.MainActivity.$3.onClick(MainActivity.java:67)
at android.view.View.performClick(View.java:4206)
at android.view.View$PerformClick.run(View.java:17357)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.invokeNative(Native Method)
at java.lang.reflect.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

I don't know why, any answer?


Solution

  • Try this..

    I think you haven't initilize the player so that null pointer coming

    You nee to check player is null or not with this if(player != null){ }

    Elample is below

    public void playSound(int arg)
    {
        if (arg == 1)
        {
            player = MediaPlayer.create(this, R.raw.atur);
        }
    
        if(player != null){
            player.setLooping(false);
            player.start();
        }
    
        try
        {
    
            if(player != null){
                if (player.isPlaying()) 
                {
                    player.stop();
                    player.release();
                }
            }
        }
        catch(Exception e)
        {
    
        }
    
    }
    

    EDIT:

     @Override
     protected void onPause()
     {
           if(player != null){
                if (player.isPlaying()) 
                {
                    player.stop();
                    player.release();
                }
            }
     }