2012-08-22 74 views
0

我正在使用媒體播放器播放mp3文件。但是當我運行它時,出現空指針異常。請告訴我是什麼問題。使用媒體播放器時出現異常

這是代碼:

package com.example.soundplayer; 

import android.app.Activity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends Activity { 

    /** 
    * Variables 
    */ 
    MediaPlayer mp = null; 
    String hello = "Hello!"; 

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

     /** 
     * Talking with the buttonHello 
     */ 
     final Button buttonHello = (Button) findViewById(R.id.idHello); 
     buttonHello.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       managerOfSound(hello); 
      } // END onClick() 
     }); // END buttonHello 
    } // END onCreate() 

    /** 
    * Manager of Sounds 
    */ 
    protected void managerOfSound(String theText) { 
     if (mp != null) { 
      mp.reset(); 
      mp.release(); 
     } 
     if (theText == hello){ 
      mp = new MediaPlayer(); 
      mp = MediaPlayer.create(MainActivity.this, R.raw.sound); 
      mp.start();} 
    } 
} 

這是例外:

08-22 13:13:01.001: W/dalvikvm(785): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
08-22 13:13:01.001: E/AndroidRuntime(785): Uncaught handler: thread main exiting due to uncaught exception 
08-22 13:13:01.020: E/AndroidRuntime(785): java.lang.NullPointerException 
08-22 13:13:01.020: E/AndroidRuntime(785): at com.example.soundplayer.MainActivity.managerOfSound(MainActivity.java:57) 
08-22 13:13:01.020: E/AndroidRuntime(785): at com.example.soundplayer.MainActivity$1.onClick(MainActivity.java:30) 
08-22 13:13:01.020: E/AndroidRuntime(785): at android.view.View.performClick(View.java:2364) 
08-22 13:13:01.020: E/AndroidRuntime(785): at android.view.View.onTouchEvent(View.java:4179) 
08-22 13:13:01.020: E/AndroidRuntime(785): at android.widget.TextView.onTouchEvent(TextView.java:6541) 
08-22 13:13:01.020: E/AndroidRuntime(785): at android.view.View.dispatchTouchEvent(View.java:3709) 
08-22 13:13:01.020: E/AndroidRuntime(785): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
08-22 13:13:01.020: E/AndroidRuntime(785): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
08-22 13:13:01.020: E/AndroidRuntime(785): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
08-22 13:13:01.020: E/AndroidRuntime(785): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
08-22 13:13:01.020: E/AndroidRuntime(785): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659) 
08-22 13:13:01.020: E/AndroidRuntime(785): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) 
08-22 13:13:01.020: E/AndroidRuntime(785): at android.app.Activity.dispatchTouchEvent(Activity.java:2061) 
08-22 13:13:01.020: E/AndroidRuntime(785): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643) 
08-22 13:13:01.020: E/AndroidRuntime(785): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691) 
08-22 13:13:01.020: E/AndroidRuntime(785): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-22 13:13:01.020: E/AndroidRuntime(785): at android.os.Looper.loop(Looper.java:123) 
08-22 13:13:01.020: E/AndroidRuntime(785): at android.app.ActivityThread.main(ActivityThread.java:4363) 
08-22 13:13:01.020: E/AndroidRuntime(785): at java.lang.reflect.Method.invokeNative(Native Method) 
08-22 13:13:01.020: E/AndroidRuntime(785): at java.lang.reflect.Method.invoke(Method.java:521) 
08-22 13:13:01.020: E/AndroidRuntime(785): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
08-22 13:13:01.020: E/AndroidRuntime(785): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
08-22 13:13:01.020: E/AndroidRuntime(785): at dalvik.system.NativeStart.main(Native Method) 

我在這一行越來越異常:

mp.start(); 

在此先感謝。

回答

2

你必須讓從創建方法的結果,並使用媒體播放器,因爲創建函數會返回媒體播放器之前,檢查結果如果成功創建對象,並且如果創建媒體播放器失敗,則返回NULL。

check the description of that function in official site

a MediaPlayer object, or null if creation failed

還有一件事你不用叫準備()函數,因爲On successfully created MediaPlayer , prepare() will already have been called and must not be called again.

可以查看到MP爲空或不是調用mp.start

if(mp!=null) 
    mp.start(); 
+0

如何做到這一點先生?你可以給我幾行代碼來理解這一點。 –

+1

謝謝,先生,現在正在工作。 –

+0

也看看這個描述http://developer.android.com/guide/topics/media/mediaplayer.html#preparingasync – rajpara

1

根據MediaPlayer狀態圖,您必須在mp.start()之前撥打mp.prepare()

enter image description here

0

試試;

mp = MediaPlayer.create(MainActivity.this, R.raw.sound); 
if(mp!=null) { 
    mp.start(); 
} 
+0

仍然顯示同樣的問題先生。現在例外是在這個line:mp.prepare(); –

0

這可能不是問題,但你應該改變這樣的:

if (theText.equals(" hello"){ 
    mp = MediaPlayer.create(MainActivity.this, R.raw.sound); 
    mp.prepare(); 
    mp.start();} 
+0

對不起,先生,但問題仍然是一樣的。 –

+0

順便說一句,你的文件夾中的文件sound.mp3? –

+0

是先生。它存在於res/raw文件夾中。 –