- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我使用了我的 Fabric
服务,并在大多数运行我的应用程序的设备上发现了这个错误。
错误是这样的:
Fatal Exception: java.lang.NullPointerException Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference
这是我的代码:
public class CameraActivity extends Activity implements SurfaceHolder.Callback{
android.hardware.Camera camera;
int moi = 0;
@InjectView(R.id.s)
SurfaceView surfaceView;
@InjectView(R.id.takeaphoto)
ImageView imageView;
SurfaceHolder surfaceHolder;
private Camera.Face[] mFaces;
// Draw rectangles and other fancy stuff:
private FaceOverlayView mFaceView;
private int mOrientation;
private float x1,x2;
static final int MIN_DISTANCE = 150;
private int mOrientationCompensation;
private OrientationEventListener mOrientationEventListener;
// Let's keep track of the display rotation and orientation also:
private int mDisplayRotation;
private int mDisplayOrientation;
Camera.PictureCallback callback;
int cameraId = 0;
Camera.ShutterCallback shutterCallback;
private Camera.FaceDetectionListener faceDetectionListener = new Camera.FaceDetectionListener() {
@Override
public void onFaceDetection(Camera.Face[] faces, Camera camera) {
Log.d("onFaceDetection", "Number of Faces:" + faces.length);
// Update the view now!
mFaceView.setFaces(faces);
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Fabric.with(this, new Crashlytics());
Fabric.with(this, new Answers());
setContentView(R.layout.camera_activity);
mFaceView = new FaceOverlayView(this);
addContentView(mFaceView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// Create and Start the OrientationListener:
mOrientationEventListener = new SimpleOrientationEventListener(this);
mOrientationEventListener.enable();
SharedPreferences pap = this.getSharedPreferences(
"AD1", Context.MODE_PRIVATE);
boolean frr = pap.getBoolean("fr", false);
if (!frr) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Tip");
builder.setMessage("Use the beautiful filters by swiping from right to left")
.setCancelable(true)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences e = getApplicationContext().getSharedPreferences(
"AD1", Context.MODE_PRIVATE);
SharedPreferences.Editor d = e.edit();
d.putBoolean("fr", true);
d.commit();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
}
ButterKnife.inject(this);
surfaceHolder=surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cameraImage();
}
});
callback = new android.hardware.Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes, android.hardware.Camera camera) {
FileOutputStream outputStream=null;
File file_image = getDirc();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyymmddhhmms");
String date = simpleDateFormat.format(new Date());
String photo_file="PI_"+date+".jpg";
String file_name = file_image.getAbsolutePath()+"/"+photo_file;
File picfile=new File(file_name);
try {
outputStream=new FileOutputStream(picfile);
outputStream.write(bytes);
outputStream.close();
}catch (FileNotFoundException e){}
catch (IOException ex){}
finally {
}
refreshCamera();
refreshGallery(picfile);
try {
camera.stopPreview();
}catch (Exception e){}
try{
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){}
}
};
}
private void refreshGallery(File file){
Intent intent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
}
public void refreshCamera(){
if (surfaceHolder.getSurface() == null){
return;
}
}
private class SimpleOrientationEventListener extends OrientationEventListener {
public SimpleOrientationEventListener(Context context) {
super(context, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onOrientationChanged(int i) {
// We keep the last known orientation. So if the user first orient
// the camera then point the camera to floor or sky, we still have
// the correct orientation.
if (orientation == ORIENTATION_UNKNOWN) return;
mOrientation = Util.roundOrientation(orientation, mOrientation);
// When the screen is unlocked, display rotation may change. Always
// calculate the up-to-date orientationCompensation.
int orientationCompensation = mOrientation
+ Util.getDisplayRotation(CameraActivity.this);
if (mOrientationCompensation != orientationCompensation) {
mOrientationCompensation = orientationCompensation;
mFaceView.setOrientation(mOrientationCompensation);
}
}
}
private File getDirc(){
File dics = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
return new File(dics ,"Camera");
}
public void cameraImage(){
camera.takePicture(null , null ,callback);
MediaPlayer mediaPlayer = MediaPlayer.create(this , R.raw.sound);
mediaPlayer.start();
}
public void surfaceCreated(SurfaceHolder surfaceHolder ) {
try {
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
parameters = camera.getParameters();
camera.setFaceDetectionListener(faceDetectionListener);
camera.startFaceDetection();
parameters.setPreviewFrameRate(20);
List<Camera.Size> customSizes = parameters.getSupportedPreviewSizes();
Camera.Size customSize = customSizes.get(0); //Added size
parameters.setPreviewSize(customSize.width, customSize.height);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
refreshCamera();
setDisplayOrientation();
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
camera.stopPreview();
camera.release();
camera=null;
}
public void frontCamera(View view) {
camera.stopPreview();
camera.release();
camera=null;
if (cameraId == 0){
try {
camera = android.hardware.Camera.open(1);
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
camera.setFaceDetectionListener(faceDetectionListener);
camera.startFaceDetection();
parameters = camera.getParameters();
parameters.setPreviewFrameRate(20);
List<Camera.Size> customSizes = parameters.getSupportedPreviewSizes();
Camera.Size customSize = customSizes.get(0); //Added size
parameters.setPreviewSize(customSize.width, customSize.height);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 1;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
else{
try {
camera = android.hardware.Camera.open();
}catch (RuntimeException ex){}
android.hardware.Camera.Parameters parameters;
camera.setFaceDetectionListener(faceDetectionListener);
camera.startFaceDetection();
parameters = camera.getParameters();
parameters.setPreviewFrameRate(20);
List<Camera.Size> customSizes = parameters.getSupportedPreviewSizes();
Camera.Size customSize = customSizes.get(0); //Added size
parameters.setPreviewSize(customSize.width, customSize.height);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
cameraId = 0;
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
}catch (Exception e){
}
}
}
private void setDisplayOrientation() {
// Now set the display orientation:
mDisplayRotation = Util.getDisplayRotation(CameraActivity.this);
mDisplayOrientation = Util.getDisplayOrientation(mDisplayRotation, 0);
camera.setDisplayOrientation(mDisplayOrientation);
if (mFaceView != null) {
mFaceView.setDisplayOrientation(mDisplayOrientation);
}
}
这是我的 list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.camera">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/pc"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashScreen"
android:theme="@style/AppTheme"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CameraActivity"
android:screenOrientation="portrait"
>
</activity>
<meta-data
android:name="io.fabric.ApiKey"
android:value="e7ded6b46068e619fd1d96f6b9eeaac888fe83f5" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
最佳答案
检查设备设置
中的应用程序,是否已授予所需的权限。在我的例子中,这是 Exception
的原因,我没有从 Settings
中授予权限。
希望对你有帮助。
关于java - 空对象引用上的 Android Camera : Attempt to invoke virtual method Android Camera Parameters on android. hardware.Camera.getParameters()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41019055/
进程虚拟机和系统虚拟机有什么区别? 我的猜测是,进程 VM 没有为该操作系统的整个应用程序提供一种操作系统,而是为某些特定应用程序提供环境。 系统虚拟机为操作系统提供了一个安装环境,就像 Virtua
我在成员函数的上下文中理解 virtual,例如 virtual void frob()。但它在类声明的上下文中意味着什么,例如 class Foo : public virtual Bar? 对于给
根据 react-virtualized 文档,“AutoSizer 组件装饰 React 元素并自动管理宽度和高度属性,以便装饰元素填充可用空间”。 建议通常是加上height: 100%;或 fl
根据 this类似 StackOverflow 问题和其他文章,默认情况下 C# 方法是“非虚拟的”,我认为这意味着您不能在派生类中覆盖它们。 如果那是真的,能否请您向我解释一下,在下面的示例中,我如
我有一个基类Media和几个派生类,即DVD、Book等...基类写成: class Media{ private: int id; string title;
我搜索了一些关于虚函数声明的帖子,相信 =0 在 virtual void test()=0; 是固定句法所以 virtual void test()=NULL; virtual void test(
我正在使用 RV 列表加载具有自定义格式的大型文档。它非常有效,但我遇到了以下两个问题: 我目前在 cellmeasurer 中设置了一个列表 based on this计算行的动态高度(宽度是固定的
我一直在努力制作 this react virtualized table example工作 & 开始严重怀疑我的理智。我创建了一个 react 应用程序,我只是想在 App.js 中使用以下内容呈
我在Windows 7 Pro计算机上安装了Windows Virtual PC和Windows XP Mode。运行XP模式会在Virtual PC上自动安装XP。我想创建第二台与第一台相同的虚拟P
我使用 Virtual PC 来创建新的环境来测试我的安装程序。但我一定是做错了什么,因为内部装有 Vista 或 XP 的 VPC 镜像占用了大约 15GB 的磁盘空间(包括安装在其中的 VS200
是否可以为 Ubuntu 虚拟机动态分配处理器和内存?例如。进程在主机系统上运行,导致处理器的使用率从 30%-70% 上下波动,这些进程还占用 8GB 内存中 3GB-7GB 之间的波动量,即 1G
我正在使用“react-virtualized”来创建一个表。在该表中,一些数据可能显示为 'Brian Vaughn1'。 .此表格单元格应具有 font-weight: bold并且只应呈现文本,
我正在使用“react-virtualized”来创建一个表。在该表中,一些数据可能显示为 'Brian Vaughn1'。 .此表格单元格应具有 font-weight: bold并且只应呈现文本,
我一直在努力理解一段这样的代码: class A { // some class definition } class B { public: virtual A *s
基于 http://en.wikipedia.org/wiki/Virtual_inheritance class Animal { ... }; // Two classes virtually i
我看到 C++ 中的某些函数被声明为 virtual const int getNumber(); 但是如果函数声明如下有什么区别呢? const virtual int getNumber(); 这
问题来自C++ faq。 http://www.parashift.com/c++-faq-lite/protected-virtuals.html 使用公共(public)重载虚拟的代码: clas
这个问题在这里已经有了答案: How is "=default" different from "{}" for default constructor and destructor? (3 个答案
virtual final 函数(final 在基类)是否有任何 vtable/virtual 成本? class B{ public: virtual void fFinal() final
我有一个只包含 exe 文件(没有源代码)的 hello 工具。 你好工具结构: bin helloBin.exe helloRoot.exe conanfile.py conanfile.py
我是一名优秀的程序员,十分优秀!