我正在尝试启动一个简单的相机应用程序。我使用了 Google 提供的示例 CameraPreview 代码。甚至下面的代码也取自 android 开发者网站上的文档。但是添加视图 mPreview 有问题。它在以下两种情况下失败 - 它说应用程序意外停止,请重试 - 也没有日志:
i) preview.addView(mPreview) 和
ii)setContentView(mPreview)
我尝试了选项 ii) 只是为了消除 CameraPreview 代码出现问题的可能性。
但是选项 i) 甚至对 Capture Button 也不起作用。所以我想知道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>