Android应用程序强制关闭SD卡音频负载

时间:2014-05-19 20:44:08

标签: java android media-player

我正在尝试加载存储在我的SD卡上的mp3文件。我正在使用HTC One手机,这就是为什么路径看起来很有趣,但我90%是正确的。

当我开始活动page2.java时,有人可以帮我弄清楚为什么应用程序会强行关闭吗?

这是我的代码

   public class page2 extends Activity {
    MediaPlayer mediaPlayer;

    boolean isPlaying = false;  


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.page2);

        try {

            mediaPlayer.setDataSource("/storage/emulated/0/Audio/sounds/first.mp3");
        } catch (IllegalArgumentException | SecurityException
                | IllegalStateException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

         mediaPlayer.start();

        final ImageView Play_button = (ImageView)findViewById(R.id.playbutton);


                Play_button.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                  if(isPlaying){
                      Play_button.setImageResource(R.drawable.stopicon);

                       isPlaying = false;

                  }else {
                      Play_button.setImageResource(R.drawable.playicon);


                       isPlaying = true;
                  }
                 }
                });

    }







}

这是我的日志

05-19 21:33:54.337: I/dalvikvm-heap(23333): Grow heap (frag case) to 20.389MB for 7322452-byte allocation
05-19 21:33:54.417: I/dalvikvm-heap(23333): Grow heap (frag case) to 27.376MB for 7322452-byte allocation
05-19 21:33:54.497: I/dalvikvm-heap(23333): Grow heap (frag case) to 34.365MB for 7322452-byte allocation
05-19 21:33:55.438: I/dalvikvm-heap(23333): Grow heap (frag case) to 129.877MB for 26873872-byte allocation
05-19 21:33:56.649: I/dalvikvm-heap(23333): Grow heap (frag case) to 155.560MB for 26873872-byte allocation
05-19 21:33:56.870: I/dalvikvm-heap(23333): Grow heap (frag case) to 164.561MB for 9437200-byte allocation
05-19 21:33:56.940: W/dalvikvm(23333): threadid=1: thread exiting with uncaught exception (group=0x416a0e18)
05-19 21:33:56.950: E/AndroidRuntime(23333): FATAL EXCEPTION: main
05-19 21:33:56.950: E/AndroidRuntime(23333): Process: com.example.app, PID: 23333
05-19 21:33:56.950: E/AndroidRuntime(23333): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.page2}: java.lang.NullPointerException
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.ActivityThread.access$800(ActivityThread.java:156)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.os.Looper.loop(Looper.java:157)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.ActivityThread.main(ActivityThread.java:5872)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at java.lang.reflect.Method.invokeNative(Native Method)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at java.lang.reflect.Method.invoke(Method.java:515)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at dalvik.system.NativeStart.main(Native Method)
05-19 21:33:56.950: E/AndroidRuntime(23333): Caused by: java.lang.NullPointerException
05-19 21:33:56.950: E/AndroidRuntime(23333):    at com.example.app.page2.onCreate(page2.java:31)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.Activity.performCreate(Activity.java:5312)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
05-19 21:33:56.950: E/AndroidRuntime(23333):    ... 11 more
05-19 21:34:02.676: D/Process(23333): killProcess, pid=23333
05-19 21:34:02.676: D/Process(23333): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:131 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 

2 个答案:

答案 0 :(得分:1)

var条件之前初始化try声明,如下所示:

mediaPlayer = new MediaPlayer();  

这将创建由之前创建的MediaPlayer检索到的新var

答案 1 :(得分:0)

您的MediaPlayernull

mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);

Using MediaPlayer