1

在我的应用程序中,我使用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
4

2 回答 2

0

尝试通过添加一个额外的参数来改变你的调用 inflate :

mPage1 = inflater.inflate(R.layout.page1, null, false);
于 2012-05-10T09:44:18.490 回答
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);

}
于 2012-12-18T14:27:27.200 回答