2012-05-10 6 views
1

私のアプリケーションではViewPagerを使用しています。私はいくつかの空白のページを作成しました。ページのプレビューでViewPager、development.android.comの説明に従ってカメラを設置します。しかし、私はアプリケーションを実行しようとすると失敗します。この問題を解決するには?クラッシュアンドロイドViewPager +カメラ

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 
+0

DDMSからエラーを貼り付けます。 – Jokahero

+0

例を見た場所から正確なリンクを教えてもらえますか? –

答えて

0

このような追加のパラメータを追加することによって膨張させるように、あなたの呼び出しを変更して試してみてください。

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

効果なし、変更出力DDMSなし。 – RogerW

+0

CamActivity.javaの97行目は何ですか? – Jokahero

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); 

}