gpt4 book ai didi

android - 应用程序一旦创建就进入 onPause

转载 作者:行者123 更新时间:2023-11-29 21:10:15 28 4
gpt4 key购买 nike

我有一个奇怪的问题。我正在编写一个继续拍照的应用程序。该应用程序运行良好,但是在实现 onPause 方法后它没有启动。我检查过它一旦创建就进入了 onPause。这是我的代码:

public class MyCamera extends Activity {
private Camera mCamera;
private CameraPreview mCameraPreview;
private FrameLayout preview;
/* Remaining stuff:
1. Implement Onpause to release camera and onResume reopen it
2. Save timer to stop it ion onpauseoonPause()
3. rotate images
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCamera = getCameraInstance();
mCameraPreview = new CameraPreview(this, mCamera);
mCamera.setDisplayOrientation(90);
//FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);

Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// mCamera.takePicture(null, null, mPicture);

// 5000ms=5s at intervals of 1000ms=1s so that means it lasts 5 seconds


/*new CountDownTimer(5000,1000){

@Override
public void onFinish() {

}

@Override
public void onTick(long millisUntilFinished) {
mCamera.takePicture(null, null, mPicture);

}

}.start();*/


Timer timer = new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run()
{
mCamera.startPreview();
mCamera.takePicture(null, null, mPicture);
try {
Thread.sleep(500);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
mCamera.startPreview();
}
}, 0, 5000);

}

});
}

/**
* Helper method to access the camera returns null if it cannot get the
* camera or does not exist
*
* @return
*/
private Camera getCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();
} catch (Exception e) {
// cannot get camera or does not exist
}
return camera;
}

PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
//createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter);
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {

} catch (IOException e) {
}
}

};

private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");

return mediaFile;
}



@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
Log.d("LifeCycle", "In the onPause() event");
// Release the Camera because we don't need it when paused
// and other activities might need to use it.
preview.removeAllViews();
if (mCamera != null) {
mCamera.release();
mCamera = null;
}

}

/*@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
setContentView(R.layout.main);
// Get the Camera instance as the activity achieves full user focus
if (mCamera == null) {
mCamera = getCameraInstance();
mCameraPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);
mCamera.startPreview();

}
}*/

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_camera, menu);
return true;
}

}

这是 LogCat 窗口:

04-17 14:44:11.954: I/dalvikvm(7444): Debugger is active
04-17 14:44:12.104: I/System.out(7444): Debugger has connected
04-17 14:44:12.104: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:12.314: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:12.514: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:12.714: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:12.914: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:13.114: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:13.314: I/System.out(7444): waiting for debugger to settle...
04-17 14:44:13.514: I/System.out(7444): debugger has settled (1453)
04-17 14:44:13.784: D/dalvikvm(7444): Note: class Lcom/lge/mdm/manager/ILGMDMDevicePolicyManager$Stub; has 319 unimplemented (abstract) methods
04-17 14:44:14.274: D/LifeCycle(7444): In the onPause() event
04-17 14:44:14.364: I/Adreno-EGL(7444): <qeglDrvAPI_eglInitialize:385>: EGL 1.4 QUALCOMM build: ()
04-17 14:44:14.364: I/Adreno-EGL(7444): OpenGL ES Shader Compiler Version: E031.24.00.02
04-17 14:44:14.364: I/Adreno-EGL(7444): Build Date: 01/20/14 Mon
04-17 14:44:14.364: I/Adreno-EGL(7444): Local Branch: PMH2-KK_3.5-RB1-AU61-554722-586267-set2
04-17 14:44:14.364: I/Adreno-EGL(7444): Remote Branch:
04-17 14:44:14.364: I/Adreno-EGL(7444): Local Patches:
04-17 14:44:14.364: I/Adreno-EGL(7444): Reconstruct Branch:
04-17 14:44:14.394: D/OpenGLRenderer(7444): Enabling debug mode 0
04-17 14:44:14.494: I/ActivityManager(7444): Timeline: Activity_idle id: android.os.BinderProxy@428b55e0 time:4511859
04-17 14:44:14.504: I/ActivityManager(7444): Timeline: Activity_idle id: android.os.BinderProxy@428b55e0 time:4511861

有什么想法吗?谢谢

最佳答案

onPause():

当 Activity 进入后台但尚未(尚未)被终止时,作为 Activity 生命周期的一部分调用。 onResume() 的对应项。当 Activity B 在 Activity A 之前启动时,将在 Activity 上调用此回调。在 A 的 onPause() 返回之前不会创建 B,因此请确保不要在此处做任何冗长的事情。

可能的原因---

  • 你正在以某种方式调用一些保持当前状态的其他 Activity 后台 Activity 。在不同的点检查您的代码。

  • 您以某种方式自行终止了当前 Activity ,所以这是在 Activity 被标记为被杀死之前调用。

关于android - 应用程序一旦创建就进入 onPause,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23132147/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com