3

我写了以下一段代码。它用于创建相机播放器。我在诺基亚手机上测试过。它工作正常,我可以看到相机并使用它的功能。

但问题是,当在三星手机上测试代码时,它会引发媒体异常,最终不得不退出应用程序。由于此代码,我的内置相机功能(即在三星手机上)也停止工作。那么它的原因是什么?

    public void startCamera()
{
    try
    {
        try 
        {
            // if player==null
            player = createPlayer();
        } 
        catch (Exception ex) 
        {
            ex.printStackTrace();
            ErrorDialog.show(ex, "We are exiting the application.",
                        "Exit", new com.auric.qrev.scanqrcode.lwuit.ui.Action(){
                    public void actionPerformed() {
                        m_objMIDlet.exitApp();
                    }
            });
        }
        try 
        {
            player.realize(); 
            player.prefetch();
        }
        catch (MediaException ex) 
        {
            ex.printStackTrace();
            ErrorDialog.show(ex, "We are exiting the application.",
                        "Exit", new com.auric.qrev.scanqrcode.lwuit.ui.Action(){
                    public void actionPerformed() {
                        m_objMIDlet.exitApp();
                    }
            });
        }




        //Grab the video control and set it to the current display.
        videoControl = (VideoControl)(player.getControl("VideoControl"));
        if (videoControl == null)
        {
            //discardPlayer();
            stopCamera();
            ErrorDialog.show("Unsupported:\n"+
                 "Can't get video control\n"+
                 "We are exiting the application.",
                    "Exit", new com.auric.qrev.scanqrcode.lwuit.ui.Action(){
                public void actionPerformed() {
                    m_objMIDlet.exitApp();
                }
            });
        }

        mediaComponent = new MediaComponent(player);
        mediaComponent.setFocusable(false);
        m_cameraScreen.showCamera(mediaComponent);
        start();
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
        //discardPlayer();
        stopCamera();
        ErrorDialog.show("Sorry,Resources unavailable.\nWe are exiting the application.",
                        "Exit", new com.auric.qrev.scanqrcode.lwuit.ui.Action(){
                    public void actionPerformed() {
                        m_objMIDlet.exitApp();
                    }
        });
    }

}

    private Player createPlayer()throws MediaException, Exception 
{
    Player mPlayer = null;
    // try capture://image first for series 40 phones
    try 
    {
        mPlayer = Manager.createPlayer("capture://image");
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    catch (Error e) 
    {
        e.printStackTrace();
    }

    // if capture://image failed, try capture://video
    if (mPlayer == null) 
    {
        try 
        {
            mPlayer = Manager.createPlayer("capture://video");
        } 
        catch (Exception e)
        {
            e.printStackTrace();
            throw new MediaException("Sorry,Resources unavailable.");
        }
    }

    if(mPlayer == null)
        throw new Exception("Sorry,Resources unavailable.");

    return mPlayer;

}

4

1 回答 1

1

首先,您必须意识到,printStackTrace()除非您使用的是 Symbian 手机,否则在模拟器之外几乎没有用处。

您也可以使用java.lang.Throwable而不是分隔ExceptionError

您可以通过在测试时将信息收集为 aString并将其附加到简单的 lcdui来准确了解发生了什么Form


try {
    // do something that could potentially fail
} catch (Throwable th) {
    myDebugLcduiForm.append("potential failure number xx." + th + th.getMessage());
    // if necessary, throw a new RuntimeException
}

一旦您确切知道哪行代码引发了什么异常,您可能想要更新/重新发布您的问题。

于 2012-05-11T13:00:44.110 回答