Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android-无法解决方法getSystemService问题_Java_Android_Android Studio_Methods_Resolve - Fatal编程技术网

Java android-无法解决方法getSystemService问题

Java android-无法解决方法getSystemService问题,java,android,android-studio,methods,resolve,Java,Android,Android Studio,Methods,Resolve,我遇到了有关getSystemService方法的问题,因为Android Studio IDE一直告诉我它无法解决此方法。据我所知,可能是因为我的类不包含活动。我正在阅读有关getSystemService问题的相关主题,但我根本找不到任何匹配的解决方案 我把代码放在上面 import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import an

我遇到了有关getSystemService方法的问题,因为Android Studio IDE一直告诉我它无法解决此方法。据我所知,可能是因为我的类不包含活动。我正在阅读有关getSystemService问题的相关主题,但我根本找不到任何匹配的解决方案

我把代码放在上面

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private Camera mCamera;
private CameraView mCameraView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
protected void onResume(){
    super.onResume();

    try{
        mCamera = Camera.open(1);
        mCameraView = new CameraView(this, mCamera);
        setContentView(mCameraView);
    } catch (Exception e){
        finish();
    }
}

@Override
protected void onPause(){
    if(mCamera != null){
        mCamera.release();
        mCamera = null;
    }
    super.onPause();


   }

}
上面这堂课的粗体部分就是我想弄明白的部分

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Surface;
import android.view.Display;
import android.view.WindowManager;
import java.io.IOException;
import android.app.Activity;
import java.util.List;


public class CameraView extends SurfaceView implements SurfaceHolder.Callback{


private Camera mCamera;
private View mView;
private WindowManager display;


public CameraView(Context context, Camera mCamera) {
    super(context);

    this.mCamera = mCamera;
    mCamera.setDisplayOrientation(90);

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}


@Override
public void surfaceCreated(SurfaceHolder holder) {
    try{
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.e("cameraPreview", "The failure of the camera settings");
    }
}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Camera.Parameters params = mCamera.getParameters();
    List<Camera.Size> sizes = params.getSupportedPreviewSizes();
    Camera.Size optionalSize = getOptimalPreviewSize(sizes, width, height);
    params.setPreviewSize(optionalSize.width, optionalSize.height);
    mCamera.setParameters(params);

    boolean isPreviewRunning = true;

    if (isPreviewRunning)
    {
        mCamera.stopPreview();
    }

    Parameters parameters = mCamera.getParameters();
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();  

    if(display.getRotation() == Surface.ROTATION_0)
    {
        parameters.setPreviewSize(height, width);
        mCamera.setDisplayOrientation(90);
    }

    if(display.getRotation() == Surface.ROTATION_90)
    {
        parameters.setPreviewSize(width, height);
    }

    if(display.getRotation() == Surface.ROTATION_180)
    {
        parameters.setPreviewSize(height, width);
    }

    if(display.getRotation() == Surface.ROTATION_270)
    {
        parameters.setPreviewSize(width, height);
        mCamera.setDisplayOrientation(180);
    }

    mCamera.setParameters(parameters);
    previewCamera();
}

public void previewCamera()
{
    try
    {
        mCamera.setPreviewDisplay();
        mCamera.startPreview();
        boolean isPreviewRunning = true;
    }
    catch(Exception e)
    {
        Log.d(APP_CLASS, "Cannot start preview", e);
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    mCamera.release();
    mCamera = null;
}

private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {

    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio=(double)h / w;

    if (sizes == null) return null;

    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    for (Camera.Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;


  }



}
导入android.content.Context;
导入android.hardware.Camera;
导入android.hardware.Camera.Parameters;
导入android.util.Log;
导入android.view.SurfaceHolder;
导入android.view.SurfaceView;
导入android.view.view;
导入android.view.Surface;
导入android.view.Display;
导入android.view.WindowManager;
导入java.io.IOException;
导入android.app.Activity;
导入java.util.List;
公共类CameraView扩展了SurfaceView,实现了SurfaceHolder.Callback{
私人摄像机麦卡梅拉;
私有视图;
专用窗口管理器显示;
公共摄像机视图(上下文,摄像机mCamera){
超级(上下文);
this.mCamera=mCamera;
mCamera.setDisplayOrientation(90);
SurfaceHolder holder=getHolder();
holder.addCallback(本);
夹持器。设置类型(表面夹持器。表面夹持器类型推压缓冲);
}
@凌驾
已创建的公共空白表面(表面持有人){
试一试{
mCamera.setPreviewDisplay(支架);
mCamera.startPreview();
}捕获(IOE异常){
Log.e(“cameraPreview”,“摄像头设置失败”);
}
}
@凌驾
公共空白表面更改(表面文件夹持有者、整型格式、整型宽度、整型高度){
Camera.Parameters params=mCamera.getParameters();
列表大小=params.getSupportedPreviewSizes();
Camera.Size optionalSize=getOptimalPreviewSize(尺寸、宽度、高度);
参数setPreviewSize(optionalSize.width、optionalSize.height);
mCamera.setParameters(参数);
布尔值isPreviewRunning=true;
如果(iPreviewRunning)
{
mCamera.stopPreview();
}
Parameters=mCamera.getParameters();
Display Display=((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation()==Surface.ROTATION\u 0)
{
参数.setPreviewSize(高度、宽度);
mCamera.setDisplayOrientation(90);
}
if(display.getRotation()==Surface.ROTATION_90)
{
参数。setPreviewSize(宽度、高度);
}
if(display.getRotation()==Surface.ROTATION_180)
{
参数.setPreviewSize(高度、宽度);
}
if(display.getRotation()==Surface.ROTATION_270)
{
参数。setPreviewSize(宽度、高度);
mCamera.setDisplayOrientation(180);
}
mCamera.setParameters(参数);
预览摄影机();
}
公共无效预览摄影机()
{
尝试
{
mCamera.setPreviewDisplay();
mCamera.startPreview();
布尔值isPreviewRunning=true;
}
捕获(例外e)
{
Log.d(应用程序类,“无法启动预览”,e);
}
}
@凌驾
公共空间表面覆盖(表面覆盖物持有人){
mCamera.release();
mCamera=null;
}
私人相机。大小getOptimalPreviewSize(列表大小,整数w,整数h){
最终双纵横比公差=0.1;
双目标率=(双)高/低;
如果(size==null)返回null;
照相机。大小优化大小=空;
double minDiff=double.MAX_值;
int targetHeight=h;
用于(相机尺寸:尺寸){
双倍比率=(双倍)size.width/size.height;
如果(数学abs(比率-目标比率)>纵横比公差)继续;
if(数学绝对值(尺寸高度-目标光)
你能告诉我怎样做才能避免这件事吗


谢谢

您需要在
上下文中调用
getSystemService
,因此,为什么它在
CameraView
中不起作用。您可以将已经传入的上下文存储为字段,并在此字段上调用getSystemService。请参见下面修改的代码:

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Surface;
import android.view.Display;
import android.view.WindowManager;
import java.io.IOException;
import android.app.Activity;
import java.util.List;


public class CameraView extends SurfaceView implements SurfaceHolder.Callback{


private Camera mCamera;
private View mView;
private WindowManager display;
private Context mContext;


public CameraView(Context context, Camera mCamera) {
    super(context);

    mContext = context;

    this.mCamera = mCamera;
    mCamera.setDisplayOrientation(90);

    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}


@Override
public void surfaceCreated(SurfaceHolder holder) {
    try{
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.e("cameraPreview", "The failure of the camera settings");
    }
}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Camera.Parameters params = mCamera.getParameters();
    List<Camera.Size> sizes = params.getSupportedPreviewSizes();
    Camera.Size optionalSize = getOptimalPreviewSize(sizes, width, height);
    params.setPreviewSize(optionalSize.width, optionalSize.height);
    mCamera.setParameters(params);

    boolean isPreviewRunning = true;

    if (isPreviewRunning)
    {
        mCamera.stopPreview();
    }

    Parameters parameters = mCamera.getParameters();
    Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();  

    if(display.getRotation() == Surface.ROTATION_0)
    {
        parameters.setPreviewSize(height, width);
        mCamera.setDisplayOrientation(90);
    }

    if(display.getRotation() == Surface.ROTATION_90)
    {
        parameters.setPreviewSize(width, height);
    }

    if(display.getRotation() == Surface.ROTATION_180)
    {
        parameters.setPreviewSize(height, width);
    }

    if(display.getRotation() == Surface.ROTATION_270)
    {
        parameters.setPreviewSize(width, height);
        mCamera.setDisplayOrientation(180);
    }

    mCamera.setParameters(parameters);
    previewCamera();
}

public void previewCamera()
{
    try
    {
        mCamera.setPreviewDisplay();
        mCamera.startPreview();
        boolean isPreviewRunning = true;
    }
    catch(Exception e)
    {
        Log.d(APP_CLASS, "Cannot start preview", e);
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    mCamera.release();
    mCamera = null;
}

private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {

    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio=(double)h / w;

    if (sizes == null) return null;

    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    for (Camera.Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }
    return optimalSize;


  }
导入android.content.Context;
导入android.hardware.Camera;
导入android.hardware.Camera.Parameters;
导入android.util.Log;
导入android.view.SurfaceHolder;
导入android.view.SurfaceView;
导入android.view.view;
导入android.view.Surface;
导入android.view.Display;
导入android.view.WindowManager;
导入java.io.IOException;
导入android.app.Activity;
导入java.util.List;
公共类CameraView扩展了SurfaceView,实现了SurfaceHolder.Callback{
私人摄像机麦卡梅拉;
私有视图;
专用窗口管理器显示;
私有上下文;
公共摄像机视图(上下文,摄像机mCamera){
超级(上下文);
mContext=上下文;
this.mCamera=mCamera;
mCamera.setDisplayOrientation(90);
SurfaceHolder holder=getHolder();
holder.addCallback(本);
夹持器。设置类型(表面夹持器。表面夹持器类型推压缓冲);
}
@凌驾
已创建的公共空白表面(表面持有人){
试一试{
mCamera.setPreviewDisplay(支架);
mCamera.startPreview();
}捕获(IOE异常){
Log.e(“cameraPreview”,“摄像头设置失败”);
}
}
@凌驾
公共空白表面更改(表面文件夹持有者、整型格式、整型宽度、整型高度){
Camera.Parameters params=mCamera.getParameters();
列表大小=params.getSupportedPreviewSizes();
Camera.Size optionalSize=getOptimalPreviewSize(尺寸、宽度、高度);
参数setPreviewSize(optionalSize.width,optiona)
context!!.getSystemService(Service.WINDOW_SERVICE) as WindowManager;
getActivity().getSystemService(Context.AUDIO_SERVICE);
TelephonyManager telephonyManager = TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);