崩溃android ViewPager +相机

时间:2012-05-10 09:03:45

标签: android camera android-viewpager

在我的应用程序中,我使用ViewPager。我创建了一些空白页面。在页面预览ViewPager上放置相机,如development.android.com中所述。但是当我尝试运行应用程序时,它失败了。如何解决这个问题?

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;

public CameraPreview(Context context, Camera camera) {
    super(context);
    mCamera = camera;

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, now tell the camera where to draw the preview.
    try {
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
    } catch (IOException e) {
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // empty. Take care of releasing the Camera preview in your activity.
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // If your preview can change or rotate, take care of those events here.
    // Make sure to stop the preview before resizing or reformatting it.

    if (mHolder.getSurface() == null){
      // preview surface does not exist
      return;
    }

    // stop preview before making changes
    try {
        mCamera.stopPreview();
    } catch (Exception e){
      // ignore: tried to stop a non-existent preview
    }

    // set preview size and make any resize, rotate or
    // reformatting changes here

    // start preview with new settings
    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();

    } catch (Exception e){
    }
}
}

主要活动:

public class CamActivity extends Activity {
private List<View> mPages;
    private View mPage1;
    private View mPage2;
    private View mPage3;

    private Camera mCamera;
    private CameraPreview mPreview;
    private FrameLayout preview;

    private ViewPager mPager;
    private TitlePageIndicator mTitleIndicator;

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

       initUi();

       mCamera = getCameraInstance();

       mPreview = new CameraPreview(this, mCamera);


   }


   public static Camera getCameraInstance(){
       Camera c = null;
       try {
           c = Camera.open(); // attempt to get a Camera instance
       }
       catch (Exception e){
           // Camera is not available (in use or does not exist)
       }
       return c; // returns null if camera is unavailable
   }

   private void initUi() {
       LayoutInflater inflater = LayoutInflater.from(this);
       mPages = new ArrayList<View>();

       mPage1 = inflater.inflate(R.layout.page1, null);
       mPage1.setTag(getString(R.string.capture));

       mPage2 = inflater.inflate(R.layout.page2, null);
       mPage2.setTag(getString(R.string.text2));

       mPage3 = inflater.inflate(R.layout.page3, null);
       mPage3.setTag(getString(R.string.text3));



       mPages.add(mPage1);
       mPages.add(mPage2);
       mPages.add(mPage3);

       MainPageAdapter adapter = new MainPageAdapter(mPages);
       mPager = (ViewPager) findViewById(R.id.viewpager);
       mPager.setAdapter(adapter);
       mPager.setCurrentItem(0);

       mTitleIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
       mTitleIndicator.setViewPager(mPager);
       mTitleIndicator.setCurrentItem(0);

       mTitleIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        public void onPageSelected(int position) {
            Toast.makeText(CamActivity.this, "Changed to page " + position, Toast.LENGTH_SHORT).show();
            if(position == 0) {
                preview = (FrameLayout) findViewById(R.id.camera_preview);
                preview.addView(mPreview);
            }
        }


        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }


        public void onPageScrollStateChanged(int state) {
        }
    });
}
}

DDMS输出:

05-10 09:15:44.696: D/dalvikvm(1135): GC_EXTERNAL_ALLOC freed 55K, 51% free 2682K/5379K,   external 1541K/1559K, paused 775ms
05-10 09:15:47.746: D/gralloc_goldfish(1135): Emulator without GPU emulation detected.
05-10 09:19:54.329: D/AndroidRuntime(1135): Shutting down VM
05-10 09:19:54.329: W/dalvikvm(1135): threadid=1: thread exiting with uncaught exception (group=0xb68ad4f0)
05-10 09:19:54.410: E/AndroidRuntime(1135): FATAL EXCEPTION: main
05-10 09:19:54.410: E/AndroidRuntime(1135): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.view.ViewGroup.addView(ViewGroup.java:1871)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.view.ViewGroup.addView(ViewGroup.java:1828)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.view.ViewGroup.addView(ViewGroup.java:1808)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at com.app.cam.CamActivity$1.onPageSelected(CamActivity.java:97)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at com.viewpagerindicator.TitlePageIndicator.onPageSelected(TitlePageIndicator.java:709)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:438)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:1527)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.view.View.dispatchTouchEvent(View.java:3885)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at  android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.os.Looper.loop(Looper.java:130)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at java.lang.reflect.Method.invokeNative(Native Method)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at java.lang.reflect.Method.invoke(Method.java:507)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-10 09:19:54.410: E/AndroidRuntime(1135):     at dalvik.system.NativeStart.main(Native Method)
05-10 09:20:02.030: I/Process(1135): Sending signal. PID: 1135 SIG: 9

2 个答案:

答案 0 :(得分:0)

尝试通过添加如下额外参数来更改您的通话膨胀:

mPage1 = inflater.inflate(R.layout.page1, null, false);

答案 1 :(得分:0)

if(position == 0) {
     preview = (FrameLayout) findViewById(R.id.camera_preview);
     preview.addView(mPreview);
}

在这里你试图一遍又一遍地添加你的mPreview视图,而logcat中的java显然说你不能这样做......

所以尝试在尝试再次添加之前将其删除:

if(position == 0) {
     preview = (FrameLayout) findViewById(R.id.camera_preview);
     if(preview.getChildCount() > 0)
         preview.removeAllViews();
     preview.addView(mPreview);

}