簡単なカメラアプリを立ち上げようとしています。Googleが提供するサンプルのCameraPreviewコードを使用しました。そして、以下のコードでさえ、Android開発者サイトのドキュメントから取られています。ただし、ビューmPreviewの追加には問題があります。次の2つの条件で失敗します-アプリケーションが予期せず停止したことを示しています。もう一度やり直してください-ログもありません:
i)preview.addView(mPreview)および
ii)setContentView(mPreview)
CameraPreviewコードに問題がある可能性を排除するために、オプションii)を試しました。
ただし、オプションi)は、キャプチャボタンでも機能しません。だから私はaddViewの何が問題なのか疑問に思いました。
私は多くのフォーラムを通過しましたが、この問題は見ていません。LayoutInflaterの使用も検討しましたが、ここでどのように適用されるかわかりません。
public class CameraTrialActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private Camera mCamera;
private CameraPreview mPreview;
private final String TAG = "Camera Preview";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
Button captureButton = (Button)findViewById(R.id.button_capture);
//preview.addView(captureButton);
//captureButton.setOnClickListener(this);
//setContentView(R.layout.main);
//setContentView(mPreview);
}
}
//XMLファイル
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:id="@+id/button_capture"
android:text="Capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>