- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 CameraPreview 定义在 Android Developer Guide 中到 ApiDemos/OS/Sensors 应用程序屏幕上。一半时间它按预期工作。但是,每隔一段时间我恢复此测试应用程序(前提是我通过按设备的“主页”图标暂停它)时,会记录以下异常:
06-13 14:10:17.369: D/SENSORS_TEST(11888): supported preview width x height: 640 x 480 06-13 14:10:17.369: D/SENSORS_TEST(11888): supported preview width x height: 320 x 240 06-13 14:10:17.369: D/SENSORS_TEST(11888): supported preview width x height: 176 x 144 06-13 14:10:17.600: D/dalvikvm(11888): GC_FOR_ALLOC freed 56K, 3% free 9091K/9347K, paused 22ms 06-13 14:10:17.600: I/dalvikvm-heap(11888): Grow heap (frag case) to 9.610MB for 695056-byte allocation 06-13 14:10:17.631: D/dalvikvm(11888): GC_CONCURRENT freed 1K, 3% free 9768K/10055K, paused 2ms+2ms 06-13 14:10:31.510: D/AndroidRuntime(11888): Shutting down VM 06-13 14:10:31.510: W/dalvikvm(11888): threadid=1: thread exiting with uncaught exception (group=0x40a351f8) 06-13 14:10:31.518: E/AndroidRuntime(11888): FATAL EXCEPTION: main 06-13 14:10:31.518: E/AndroidRuntime(11888): java.lang.RuntimeException: Method called after release() 06-13 14:10:31.518: E/AndroidRuntime(11888): at android.hardware.Camera.setPreviewDisplay(Native Method) 06-13 14:10:31.518: E/AndroidRuntime(11888): at android.hardware.Camera.setPreviewDisplay(Camera.java:405) 06-13 14:10:31.518: E/AndroidRuntime(11888): at com.example.sensor.Sensors10Activity$CameraPreview.surfaceCreated(Sensors10Activity.java:221)
06-13 14:10:31.518: E/AndroidRuntime(11888): at android.view.SurfaceView.updateWindow(SurfaceView.java:533) 06-13 14:10:31.518: E/AndroidRuntime(11888): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:226) 06-13 14:10:31.518: E/AndroidRuntime(11888): at android.view.View.dispatchWindowVisibilityChanged(View.java:5839) 06-13 14:10:31.518: E/AndroidRuntime(11888): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945) 06-13 14:10:31.518: E/AndroidRuntime(11888): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945) 06-13 14:10:31.518: E/AndroidRuntime(11888): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945) 06-13 14:10:31.518: E/AndroidRuntime(11888): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945) 06-13 14:10:31.518: E/AndroidRuntime(11888): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:965) 06-13 14:10:31.518: E/AndroidRuntime(11888): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) 06-13 14:10:31.518: E/AndroidRuntime(11888): at android.os.Handler.dispatchMessage(Handler.java:99) 06-13 14:10:31.518: E/AndroidRuntime(11888): at android.os.Looper.loop(Looper.java:137) 06-13 14:10:31.518: E/AndroidRuntime(11888): at android.app.ActivityThread.main(ActivityThread.java:4424) 06-13 14:10:31.518: E/AndroidRuntime(11888): at java.lang.reflect.Method.invokeNative(Native Method) 06-13 14:10:31.518: E/AndroidRuntime(11888): at java.lang.reflect.Method.invoke(Method.java:511) 06-13 14:10:31.518: E/AndroidRuntime(11888): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 06-13 14:10:31.518: E/AndroidRuntime(11888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-13 14:10:31.518: E/AndroidRuntime(11888): at dalvik.system.NativeStart.main(Native Method)
这是代码,其中大部分直接取自开发指南的相机示例和 ApiDemos/OS/Sensors 示例。有没有人看到在 Activity 生命周期中应该做些什么来避免第 221 行发生的异常(如代码中所注释)?
提前感谢您的检查,
格雷格
public class Sensors10Activity extends Activity {
private final String TAG = "SENSORS_TEST";
private SensorManager mSensorManager;
private GraphView mGraphView;
private Camera mCamera;
private CameraPreview mCameraPreview;
public class GraphView extends View implements SensorEventListener {
private Bitmap mBitmap;
private Paint mPaint = new Paint();
private Canvas mCanvas = new Canvas();
private Path mPath = new Path();
private RectF mRect = new RectF();
private float mLastValues[] = new float[3*2];
private float mOrientationValues[] = new float[3];
private int mColors[] = new int[3*2];
private float mLastX;
private float mScale[] = new float[2];
private float mYOffset;
private float mMaxX;
private float mSpeed = 1.0f;
private float mWidth;
private float mHeight;
public GraphView(Context context) {
super(context);
mColors[0] = Color.argb(192, 255, 64, 64);
mColors[1] = Color.argb(192, 64, 128, 64);
mColors[2] = Color.argb(192, 64, 64, 255);
mColors[3] = Color.argb(192, 64, 255, 255);
mColors[4] = Color.argb(192, 128, 64, 128);
mColors[5] = Color.argb(192, 255, 255, 64);
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mRect.set(-0.5f, -0.5f, 0.5f, 0.5f);
mPath.arcTo(mRect, 0, 180);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
mCanvas.setBitmap(mBitmap);
mCanvas.drawColor(0xFFFFFFFF);
mYOffset = h * 0.5f;
mScale[0] = - (h * 0.5f * (1.0f / (SensorManager.STANDARD_GRAVITY * 2)));
mScale[1] = - (h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX)));
mWidth = w;
mHeight = h;
if (mWidth < mHeight) {
mMaxX = w;
} else {
mMaxX = w-50;
}
mLastX = mMaxX;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
synchronized (this) {
if (mBitmap != null) {
final Paint paint = mPaint;
final Path path = mPath;
final int outer = 0xFFC0C0C0;
final int inner = 0xFFff7010;
if (mLastX >= mMaxX) {
mLastX = 0;
final Canvas cavas = mCanvas;
final float yoffset = mYOffset;
final float maxx = mMaxX;
final float oneG = SensorManager.STANDARD_GRAVITY * mScale[0];
paint.setColor(0xFFAAAAAA);
cavas.drawColor(0xFFFFFFFF);
cavas.drawLine(0, yoffset, maxx, yoffset, paint);
cavas.drawLine(0, yoffset+oneG, maxx, yoffset+oneG, paint);
cavas.drawLine(0, yoffset-oneG, maxx, yoffset-oneG, paint);
}
canvas.drawBitmap(mBitmap, 0, 0, null);
float[] values = mOrientationValues;
if (mWidth < mHeight) {
float w0 = mWidth * 0.333333f;
float w = w0 - 32;
float x = w0*0.5f;
for (int i=0 ; i<3 ; i++) {
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.translate(x, w*0.5f + 4.0f);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
paint.setColor(outer);
canvas.scale(w, w);
canvas.drawOval(mRect, paint);
canvas.restore();
canvas.scale(w-5, w-5);
paint.setColor(inner);
canvas.rotate(-values[i]);
canvas.drawPath(path, paint);
canvas.restore();
x += w0;
}
} else {
float h0 = mHeight * 0.333333f;
float h = h0 - 32;
float y = h0*0.5f;
for (int i=0 ; i<3 ; i++) {
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.translate(mWidth - (h*0.5f + 4.0f), y);
canvas.save(Canvas.MATRIX_SAVE_FLAG);
paint.setColor(outer);
canvas.scale(h, h);
canvas.drawOval(mRect, paint);
canvas.restore();
canvas.scale(h-5, h-5);
paint.setColor(inner);
canvas.rotate(-values[i]);
canvas.drawPath(path, paint);
canvas.restore();
y += h0;
}
}
}
}
}
public void onSensorChanged(SensorEvent event) {
//Log.d(TAG, "sensor: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
synchronized (this) {
if (mBitmap != null) {
final Canvas canvas = mCanvas;
final Paint paint = mPaint;
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
for (int i=0 ; i<3 ; i++) {
mOrientationValues[i] = event.values[i];
}
} else {
float deltaX = mSpeed;
float newX = mLastX + deltaX;
int j = (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) ? 1 : 0;
for (int i=0 ; i<3 ; i++) {
int k = i+j*3;
final float v = mYOffset + event.values[i] * mScale[j];
paint.setColor(mColors[k]);
canvas.drawLine(mLastX, mLastValues[k], newX, v, paint);
mLastValues[k] = v;
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mLastX += mSpeed;
}
invalidate();
}
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
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); // !!! LINE 221: exception occurs here.
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
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){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
//////////////
// LIFE CYCLE
//
/**
* Initialization of the Activity after it is first created. Must at least
* call {@link android.app.Activity#setContentView setContentView()} to
* describe what is to be displayed in the screen.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mCamera = Camera.open(1);
mCameraPreview = new CameraPreview(this, mCamera);
mGraphView = new GraphView(this);
// setContentView(mGraphView);
// Create RelativeLayout for layout root.
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
// Add GraphView to layout.
RelativeLayout.LayoutParams lpGraph = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
mGraphView.setLayoutParams(lpGraph);
relativeLayout.addView(mGraphView);
// Add SurfaceView to layout.
List<Camera.Size> ls = mCamera.getParameters().getSupportedPreviewSizes();
int n = ls.size();
int widthMin = 10000;
int imin = -1;
for (int i=0; i<n; i++) {
Log.d(TAG, "supported preview width x height: " + ls.get(i).width + " x " + ls.get(i).height);
if (widthMin > ls.get(i).width) {
widthMin = ls.get(i).width;
imin = i;
}
}
if (imin >= 0) {
RelativeLayout.LayoutParams lpSurface = new RelativeLayout.LayoutParams(
ls.get(imin).width, ls.get(imin).height);
lpSurface.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lpSurface.addRule(RelativeLayout.CENTER_HORIZONTAL);
mCameraPreview.setLayoutParams(lpSurface);
relativeLayout.addView(mCameraPreview);
}
// Provide Android framework with layout root.
setContentView(relativeLayout, rlp);
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(mGraphView,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mGraphView,
mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mGraphView,
mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_FASTEST);
if (mCamera == null)
mCamera = Camera.open(1);
if (mCameraPreview == null)
mCameraPreview = new CameraPreview(this, mCamera);
}
@Override
protected void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release(); // release the camera for other applications
mCamera = null;
}
if (mCameraPreview != null) {
mCameraPreview = null;
}
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(mGraphView);
super.onStop();
}
最佳答案
经过几个小时的反复试验,这些生命周期处理程序似乎起作用了(即,它们处理由电源按钮引起的暂停、由主页图标引起的停止、由后退图标引起的销毁,以及相机被释放 onPause() 以使其可用于系统的相机应用程序)。
@Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mCamera = Camera.open(1);
mCameraPreview = new CameraPreview(this, mCamera);
mGraphView = new GraphView(this);
// setContentView(mGraphView);
// Create RelativeLayout for layout root.
mLayoutRoot = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
// Add GraphView to layout.
RelativeLayout.LayoutParams lpGraph = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
mGraphView.setLayoutParams(lpGraph);
mLayoutRoot.addView(mGraphView);
// Add SurfaceView to layout.
List<Camera.Size> ls = mCamera.getParameters().getSupportedPreviewSizes();
int n = ls.size();
int widthMin = 10000;
int imin = -1;
for (int i=0; i<n; i++) {
Log.d(TAG, "supported preview width x height: " + ls.get(i).width + " x " + ls.get(i).height);
if (widthMin > ls.get(i).width) {
widthMin = ls.get(i).width;
mCameraPreviewSize = ls.get(i);
imin = i;
}
}
if (imin >= 0) {
RelativeLayout.LayoutParams lpSurface = new RelativeLayout.LayoutParams(
ls.get(imin).width, ls.get(imin).height);
lpSurface.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lpSurface.addRule(RelativeLayout.CENTER_HORIZONTAL);
mCameraPreview.setLayoutParams(lpSurface);
mLayoutRoot.addView(mCameraPreview);
}
// Provide Android framework with layout root.
setContentView(mLayoutRoot, rlp);
Log.d(TAG, "onCreate OUT mCamera, mCameraPreview: " + mCamera + ", " + mCameraPreview);
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart OUT mCamera, mCameraPreview: " + mCamera + ", " + mCameraPreview);
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(mGraphView,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mGraphView,
mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(mGraphView,
mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_FASTEST);
if (mCamera == null)
mCamera = Camera.open(1);
if (mCameraPreview == null) {
mCameraPreview = new CameraPreview(this, mCamera);
RelativeLayout.LayoutParams lpCameraPreview = new RelativeLayout.LayoutParams(
mCameraPreviewSize.width, mCameraPreviewSize.height);
lpCameraPreview.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lpCameraPreview.addRule(RelativeLayout.CENTER_HORIZONTAL);
mCameraPreview.setLayoutParams(lpCameraPreview);
mLayoutRoot.addView(mCameraPreview);
}
Log.d(TAG, "onResume OUT mCamera, mCameraPreview: " + mCamera + ", " + mCameraPreview);
}
@Override
protected void onPause() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release(); // release the camera for other applications
mCamera = null;
}
if (mCameraPreview != null) {
mLayoutRoot.removeView(mCameraPreview);
mCameraPreview = null;
}
super.onPause();
Log.d(TAG, "onPause OUT mCamera, mCameraPreview: " + mCamera + ", " + mCameraPreview);
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(mGraphView);
super.onStop();
Log.d(TAG, "onStop OUT mCamera, mCameraPreview: " + mCamera + ", " + mCameraPreview);
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy OUT mCamera, mCameraPreview: " + mCamera + ", " + mCameraPreview);
}
关于android - 在整个 Activity 生命周期中管理相机预览 SurfaceView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11022031/
我对 Android 很陌生,如果问题重复,请避免并发送链接。有三个 Activity A、B 和 C。 Activity A 获取一个用户名,我想在 Activity C 中显示该用户名,但我想先运
我正在尝试制作记事本应用程序,因此每次打开新笔记时,布局都会相同。另外, Activity 的数量(新注释)不应定义得尽可能多 最佳答案 如果 Activity 始终相同,您可能应该创建一个适配器,允
我有 3 个 Activity 。 主窗口 5 个按钮 在按钮的主窗口中按下此窗口打开(将其称为父窗口) 在父窗口按钮上按下此窗口打开调用它作为结束子窗口。 现在从子窗口我从父窗口获取值如下:
我遇到了一个 Activity backstack 问题。假设我的后台有 5 个 Activity :比如 Activity A、 Activity B、 Activity C、 Activity D
我正在寻找必须具有以下附加特征的 JMS 提供程序: 采用多代理,所有代理都必须处于事件状态(无单点故障) 仅在两台机器上进行扩展就足以满足我们的需求 能够保证订购(如果 1 个生产者 + 1 个消费
假设,我有一个由 TabHost 组成的选项卡 Activity 。 TabHost 包含 2 个选项卡,每两个选项卡都有一个 Activity 组。每个 Activity 组包含一项 Activit
我正在开发一个应用程序,我需要根据某些操作导航到特定 Activity 。这是一张图片 我的第一个 Activity 是 ReadingActivity。基于某些操作,用户将被带到 NewProjec
我创建了一个与服务器异步通信的应用程序。当应用程序发出服务器请求时,将创建一个带有“正在加载”通知的新对话框( Activity )。主要 Activity 实现了处理服务器响应的方法,我想在主要 A
我想在我的所有应用程序 Activity 中显示相同的选项菜单。我创建了一个实现菜单的通用 Activity ,并且我所有的进一步 Activity 都扩展了它。 问题:当我需要扩展其他特定 Acti
我有四个 Activity ,即 java 文件 - Activity1.java、activity2.java、activity3.java、activity4.java 和 xml 文件 - Ac
我有两个 Activity 。我想将数据从第二个 Activity 发送到上一个 Activity 。第一个 Activity 有自定义 ListView 和 bean 类。当我点击第二个 Activ
根 Activity 是堆栈中当前的第一个 Activity 还是 list 中指定为启动 Activity 的 Activity ? 支持应用程序 P 在启动时启动 Activity A。然后 A
你好 我想知道您在绘制 Activity 图选择“Activity ”时考虑了哪些关键点? 您如何从要建模的问题中选择 Activity ? 谢谢 最佳答案 Activity 图用于对正在开发的系统和
如何从主 Activity 启动 Activity 并在子 Activity 返回主 Activity 中退出操作后返回主 Activity ? 我已将子 Activity 作为启动器 Intent
我的工作流程如下: 登录 Activity -> ActivityB -> ActivityC -> ActivityD 我想将数据从LoginActivity传递到ActivityD,但不直接传递到
我之前曾尝试获得此问题的答案,但找不到可以解决我的问题的答案。我正在制作保存圆盘高尔夫球分数的应用程序。我的 MainActivity 有 4 个按钮。新比赛、恢复比赛、类(class)和球员。 At
我有一个 tts 非 UI 类和 Activity 类。现在在 Activity 类中,我有一个按钮,用户可以从中选择男声或女声,具体取决于我想要将字符串传递给 tts 类的选择,然后一次tts 类根
问题有点复杂,首先, Activity A 和 Activity B 的 list 中都有 android:noHistory = true 。我有一个自定义 serialized 类,假设 MyCl
在我的应用程序中,我有两个 Activity (AuthenticationActivity 和 MainActivity),每个 Activity 都有一个导航图和大量 fragment 。我创建了
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How can i use compose email activity in tabView? 我想在选项
我是一名优秀的程序员,十分优秀!