在运行时删除Surface

在运行时删除Surface

我是一个小孩子,但是有很多工作要做。我的布局上有一个图片按钮。在单击它应该开始显示摄像机预览。
 (我有一个视频同时在另一个表面上显示。)
两者都很好。

但是当再次按下按钮时,预览应该停止并且应该移除表面。我正在尝试,但无法去除表面。总是得到一个空指针异常。

这是我的点击功能

public void DispPreview(View view) {
if(camPreview == null ) {
    camPreview = new PreviwDiaplay(this,fl);
} else {
    //camPreview.mSurfaceView.setVisibility(View.GONE);
    fl.removeView(camPreview.mSurfaceView);
    camPreview.stopPreview();
    camPreview = null;
}


}

这是我的预览显示类的代码.....

      public class PreviwDiaplay extends ViewGroup implements SurfaceHolder.Callback{
    Camera mCamera;
    SurfaceHolder mHolder;
    public SurfaceView mSurfaceView;
    FrameLayout fl1;
    public PreviwDiaplay(Context context) {
        super(context);
    }
    public PreviwDiaplay(Context context, FrameLayout fl2) {
        super(context);
        fl1 = fl2;
        mSurfaceView = new SurfaceView(context);
         fl2.addView(mSurfaceView,160,120);
         System.out.println("Added surface view........................");
         mHolder = mSurfaceView.getHolder();
        // TODO Auto-generated constructor stub
        try {
            mCamera = Camera.open(0);
        } catch(RuntimeException e) {
            e.printStackTrace();
            System.out.println("Camera not found");
            return;
        }
        Parameters p = mCamera.getParameters();
        p.setPictureSize(120, 90);
        mCamera.setParameters(p);

         // Install a SurfaceHolder.Callback so we get notified when the
         // underlying surface is created and destroyed.
         mHolder.addCallback(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder sv2, int arg1, int arg2, int arg3) {
        Callback callback = null;
        // TODO Auto-generated method stub
        sv2.addCallback(callback);
        System.out.println("surface changed in preview..........");
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        System.out.println("surface created in preview..........");
        if (mCamera != null) {

            requestLayout();

            try {
                mCamera.setPreviewDisplay(mHolder);
            } catch (IOException e) {
                e.printStackTrace();
            }


            mCamera.startPreview();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        System.out.println("surfaceDestroyed.......................");

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        System.out.println("onLayout.............");

    }

    public void stopPreview() {
        if(mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
            //mSurfaceView = null;
//          fl1.removeView(mSurfaceView);
        }
    }
}


这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="@drawable/logo"
android:clickable="false" >
   <SurfaceView
    android:id="@+id/surfaceView1"
    android:layout_width="280dp"
    android:layout_height="210dp"
    android:layout_gravity="center_vertical|center_horizontal"
    android:clickable="true"
    android:onClick="BringCallerFront" />
   <RelativeLayout android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center_horizontal"
    style="?android:attr/buttonBarStyle"
    android:fadeScrollbars="true">
<ImageButton
   android:id="@+id/preview"
   style="android:imageButtonStyle"
   android:adjustViewBounds="true"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/preview_small"
   android:onClick="DispPreview"
   android:contentDescription="@string/preview"/>
      <ImageButton
       android:id="@+id/end_call"
       style="android:imageButtonStyle"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       android:adjustViewBounds="true"
       android:contentDescription="@string/end_call"
       android:onClick="FinishActivity"
       android:scaleType="center"
       android:scrollbarStyle="outsideInset"
       android:src="@drawable/end_conf_small" />
     </RelativeLayout>
</FrameLayout>


有人可以帮我这个忙吗?

谢谢 .....................

这是我的logcat信息

11-20 10:19:33.674: I/System.out(768): surfaceCreated.................
11-20 10:19:33.704: I/System.out(768): PlayerThread .................
11-20 10:19:33.854: E/WVMExtractor(768): Failed to open libwvm.so
11-20 10:19:34.154: I/OMXClient(768): Using client-side OMX mux.
11-20 10:19:34.194: E/OMXNodeInstance(768): OMX_GetExtensionIndex failed
11-20 10:19:34.204: I/System.out(768): PlayerThread going to start.................
11-20 10:19:34.294: I/Choreographer(768): Skipped 236 frames!  The application may be doing too much work on its main thread.
11-20 10:19:34.444: D/gralloc_goldfish(768): Emulator without GPU emulation detected.
11-20 10:19:34.974: I/Choreographer(768): Skipped 69 frames!  The application may be doing too much work on its main thread.
11-20 10:19:47.779: I/System.out(768): Added surface view........................
11-20 10:19:48.387: I/Choreographer(768): Skipped 158 frames!  The application may be doing too much work on its main thread.
11-20 10:19:48.594: I/System.out(768): surface created in preview..........
11-20 10:19:49.376: I/System.out(768): surface changed in preview..........
11-20 10:19:56.185: I/Choreographer(768): Skipped 35 frames!  The application may be doing too much work on its main thread.
11-20 10:19:56.384: I/System.out(768): surfaceDestroyed.......................
11-20 10:19:56.434: D/AndroidRuntime(768): Shutting down VM
11-20 10:19:56.453: W/dalvikvm(768): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-20 10:19:56.614: E/AndroidRuntime(768): FATAL EXCEPTION: main
11-20 10:19:56.614: E/AndroidRuntime(768): java.lang.IllegalStateException: Could not execute method of the activity
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.view.View$1.onClick(View.java:3591)
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.view.View.performClick(View.java:4084)
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.view.View$PerformClick.run(View.java:16966)
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.os.Handler.handleCallback(Handler.java:615)
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.os.Looper.loop(Looper.java:137)
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-20 10:19:56.614: E/AndroidRuntime(768):  at java.lang.reflect.Method.invokeNative(Native Method)
11-20 10:19:56.614: E/AndroidRuntime(768):  at java.lang.reflect.Method.invoke(Method.java:511)
11-20 10:19:56.614: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-20 10:19:56.614: E/AndroidRuntime(768):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-20 10:19:56.614: E/AndroidRuntime(768):  at dalvik.system.NativeStart.main(Native Method)
11-20 10:19:56.614: E/AndroidRuntime(768): Caused by: java.lang.reflect.InvocationTargetException
11-20 10:19:56.614: E/AndroidRuntime(768):  at java.lang.reflect.Method.invokeNative(Native Method)
11-20 10:19:56.614: E/AndroidRuntime(768):  at java.lang.reflect.Method.invoke(Method.java:511)
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.view.View$1.onClick(View.java:3586)
11-20 10:19:56.614: E/AndroidRuntime(768):  ... 11 more
11-20 10:19:56.614: E/AndroidRuntime(768): Caused by: java.lang.NullPointerException
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.view.SurfaceView.updateWindow(SurfaceView.java:527)
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:226)
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.view.View.dispatchDetachedFromWindow(View.java:11785)
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3651)
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3626)
11-20 10:19:56.614: E/AndroidRuntime(768):  at android.view.ViewGroup.removeView(ViewGroup.java:3558)
11-20 10:19:56.614: E/AndroidRuntime(768):  at ofi_codec1.ofi_codec2.ofi_vc_video_codec_java_wrap.Ofi_video_encoder_test.DispPreview(Ofi_video_encoder_test.java:153)
11-20 10:19:56.614: E/AndroidRuntime(768):  ... 14 more
11-20 10:20:00.534: I/Process(768): Sending signal. PID: 768 SIG: 9

最佳答案

终于我有了主意。

我用了

camPreview.mSurfaceView.setVisibility(View.GONE);


在我的PreviwDiaplay类的stopPreview中。
并将其放在try catch子句中。而且效果很好。
为了固定布局位置,我使用了

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,(Gravity.TOP| Gravity.RIGHT));
params.height = height;
params.width = width;
fl.addView(mSurfaceView, params);

关于android - 在运行时删除Surface,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20071061/

10-08 23:44