应用程序突然强制停止单击带声音按钮时

时间:2013-12-05 03:33:58

标签: android forceclose soundeffect

我创建了一个项目,这是MainActivity类

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;
}
}

当我尝试在Bluestacks或Galaxy S4上编译时,它正在工作,但当我尝试在平板电脑上编译时,我按下btnmulai或btninfo,它说“不幸的是,myproject已停止”.. 这是Logcat错误:

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)

我不知道为什么,任何答案?

3 个答案:

答案 0 :(得分:0)

试试:

private void playSound() {
    // TODO Auto-generated method stub
    try{
        player.release();
    }
    catch(Exception e)
    {Log.i("error Media player", e.getMessage());
    }
        player=MediaPlayer.create(this, R.raw.atur);
        player.start();
}

答案 1 :(得分:0)

试试这个..

我认为你没有初始化播放器以便空指针

您需要使用此if(player != null){ }

检查播放器是否为空

Elample在

之下
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)
    {

    }

}

修改

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

答案 2 :(得分:0)

您必须在实际调用player方法之前初始化player.isPlaying(),例如:

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

    }


    player.setLooping(false);
    player.start();
}

但是,请确保U处理arg不为1的情况,以便即使在这种情况下仍然可以初始化播放器。 (在此实现中playSound(2)之前调用playSound(1)时会崩溃