2013-06-30 26 views
0

我增添了新的界面,我可以打電話HTML Android的方法。主要活動電話html文件到我的webview和接口類來執行播放聲音的播放方法。所有的東西都運行良好,但是當我添加我的代碼以在關閉應用程序時停止聲音或者在按下回車鍵或主鍵時關閉聲音。但錯誤「應用程序已停止...」發生在按下時或按回車鍵時還旋轉。錯誤停止的應用程序和旋轉

public class x extends Activity { 

private MediaPlayer mp; 
private String TAG; 
Context mContext; 
private IntentListener listener = new IntentListener(); 
WebAppInterface wb= new WebAppInterface(mContext); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity); 


    //Call HTML Files 
    WebView myWebView = (WebView) findViewById(R.id.web_engine); 

    WebSettings webSettings = myWebView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    myWebView.loadUrl("file:///android_asset/index.html"); 

    // Intiate interface 

    myWebView.addJavascriptInterface(new WebAppInterface(this), "Android"); 


} 
@Override 
protected void onPause() 
{ 
    super.onPause(); 
    if(mp.isPlaying()) 
    mp.pause(); //stop the sound 
} 

@Override 
protected void onResume() 
{ 
    super.onResume(); 
    if(WebAppInterface.checked) 
    { 
     mp.start(); 
    } 
} 
@Override 
protected void onDestroy() 
{ 
    super.onDestroy(); 
    if(WebAppInterface.checked) 
    { 
     mp.stop(); 
    } 
} 

}

public class WebAppInterface { 
Context mContext; 
private MediaPlayer mp; 
public static boolean checked = false; 
/** Instantiate the interface and set the context */ 
WebAppInterface(Context c) { 
    mContext = c; 
} 


@JavascriptInterface 
public void playsound(String value ) { 
    if (value.equals("on")) { 
     checked = true; 
     mp= MediaPlayer.create(mContext,R.raw.sound); 
     mp.setLooping(true); 
     mp.start(); 
    } 
    else 
    { 
     checked = false; 
     mp.stop(); 
    } 
} 

}

<activity 
     android:name="com.ramadan.Ramadan" 
     android:label="@string/app_name"> 

07-01 01:06:29.628: E/AndroidRuntime(631): java.lang.RuntimeException: Unable to pause activity {com.ramadan/com.ramadan.Ramadan}: java.lang.NullPointerException 
+0

的可能重複[如何從Android中Java接口類初始化的媒體播放器?(http://stackoverflow.com/questions/17400864/how-to-initialize-media-player-from-interface-class-in -android-java) – Jerad

回答

1

編輯版本2: 從您的評論

mp is media player and it is initialized in interface , so can i initialize it on activity but from the interface 

的方式你的代碼結構是不正確的呢。你有兩個變量叫mp。因此,讓我們將變量重命名爲更清晰。

private MediaPlayer mMediaPlayerActivity; 
private MediaPlayer mMediaPlayerWebApp; 

在你的界面你初始化mMediaPlayerWebApp在這裏。

public void playsound(String value ) { 
    if (value.equals("on")) { 
     checked = true; 
     mMediaPlayerWebApp= MediaPlayer.create(mContext,R.raw.sound); 
     mMediaPlayerWebApp.setLooping(true); 
     mMediaPlayerWebApp.start(); 
    } 
    else 
    { 
     checked = false; 
     mMediaPlayerWebApp.stop(); 
    } 
} 

在你的活動你沒有初始化mMediaPlayerActivity可言。但是你嘗試在你的onDestory/onPause事件中使用它。爲了您的代碼正常工作,您需要一起刪除mMediaPlayerActivity並使用您的mMediaPlayerWebApp。你如何在活動中做到這一點?這裏

@Override 
protected void onDestroy() 
{ 
    super.onDestroy(); 
    if(WebAppInterface.checked) 
    { 
     WebAppInterface.mMediaPlayerWebApp.stop(); //change mMediaPlayer to public 
    } 
} 

現在,這是出路,我可以看到在你的代碼中的另一個潛在的問題。如果playSound()永遠不會被調用呢?當你嘗試調用mMediaPlayerWebApp.stop()時,你會得到另一個nullpointerexception,因爲它從來沒有機會去!代碼中的另一個問題是它缺少基本的OOP概念Encapsulation

所以在這裏我將如何改寫你的代碼。

public class WebAppInterface 
{ 
    private MediaPlayer mMediaPlayerWebApp; 
    private boolean mHasSoundPlaying = false; 

    WebAppInterface(Context context) 
    { 
     mMediaPlayerWebApp = MediaPlayer.create(context, R.raw.sound); 
    } 

    @JavascriptInterface 
    public void playsound(String value) 
    { 
     if (value.equals("on")) 
     { 
      setSoundPlaying(true); 
     } else 
     { 
      setSoundPlaying(false); 
     } 
    } 

    public void setSoundPlaying(Boolean isPlaying) 
    { 
     this.mHasSoundPlaying = isPlaying; 

     if (isPlaying) 
     { 
      mMediaPlayerWebApp.setLooping(true); 
      mMediaPlayerWebApp.start(); 
     } 
     else 
     { 
      if (mMediaPlayerWebApp.isPlaying()) 
      { 
       mMediaPlayerWebApp.stop(); 
      } 
     } 
    } 

public void pauseSound() 
{ 
    this.mMediaPlayerWebApp.pause(); 
} 


public Boolean getSoundPlaying() 
{ 
    return this.mHasSoundPlaying; 
} 
} 

然後在你的活動只是使用setSoundPlaying(boolean)方法來 使其停止或啓動。

現在我注意到,第三潛在的bug。在你的活動中。

WebAppInterface wb= new WebAppInterface(mContext); 

mContext也未初始化。只要改變這

WebAppInterface wb; 

然後在OnCreate()

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity); 


    //Call HTML Files 
    WebView myWebView = (WebView) findViewById(R.id.web_engine); 

    WebSettings webSettings = myWebView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    myWebView.loadUrl("file:///android_asset/index.html"); 

    // Instantiate interface 

    this.wb = new WebAppInterface(this); 

    myWebView.addJavascriptInterface(this.wb, "Android"); 
} 

總的來說,我會推薦和皮卡純Java的書。你需要熟悉一些基本的OOP概念。這些概念可以幫助您的代碼陷入NullPointerException,並且可以更輕鬆地追蹤錯誤。


原始郵件到這個問題: 我注意到的另一件事是,在你的Activty你有一些所謂

private MediaPlayer mp; 

,然後你越往下有這個

@Override 
protected void onDestroy() 
{ 
    super.onDestroy(); 
    if(WebAppInterface.checked) 
    { 
     mp.stop(); 
    } 
} 

我不無處不在MediaPlayer mp在您的活動中的任何地方進行初始化。在初始化之前,你不能調用任何方法,否則會導致nullpointerexception。

+0

mp是媒體播放器,它在界面中初始化,所以我可以初始化它的活動,但從界面 – egydeveloper

0

MediaPlayer mp未初始化。所以,你會得到NPE。

相關問題