我正在尝试集成一些代码,其中一条代码就是:
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
当我在Activity中有它时,没有错误。当我把它放在我的片段中时,我收到此错误: 预计:android.content.context 实际:这个(com.xxx.ScanFragment)
这是CameraPreview方法
public CameraPreview(Context context, Camera camera,
PreviewCallback previewCb,
AutoFocusCallback autoFocusCb)
正如我所说的,当它出现在活动文件中时没有错误,所以我认为问题是什么" context"在活动和碎片之间?
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.parse.ParseUser;
import net.sourceforge.zbar.Config;
import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;
/**
* Created by Jakub on 7/23/2014.
*/
public class Pay extends Fragment {
private Button scanBtn;
private TextView formatTxt, contentTxt;
private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;
TextView scanText;
Button scanButton;
ImageScanner scanner;
private boolean barcodeScanned = false;
private boolean previewing = true;
static {
System.loadLibrary("iconv");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pay, container, false);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
autoFocusHandler = new Handler();
mCamera = getCameraInstance();
/* Instance barcode scanner */
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);
mPreview = new CameraPreview(getActivity(), mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout)getActivity().findViewById(R.id.cameraPreview);
Log.d(Payspot.TAG,"This is the string:" + preview.toString());
preview.addView(mPreview);
scanText = (TextView)getActivity().findViewById(R.id.scanText);
scanButton = (Button)getActivity().findViewById(R.id.ScanButton);
scanButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (barcodeScanned) {
barcodeScanned = false;
scanText.setText("Scanning...");
mCamera.setPreviewCallback(previewCb);
mCamera.startPreview();
previewing = true;
mCamera.autoFocus(autoFocusCB);
}
}
});
}
public void onPause() {
super.onPause();
releaseCamera();
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
} catch (Exception e){
}
return c;
}
private void releaseCamera() {
if (mCamera != null) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
private Runnable doAutoFocus = new Runnable() {
public void run() {
if (previewing)
mCamera.autoFocus(autoFocusCB);
}
};
Camera.PreviewCallback previewCb = new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = parameters.getPreviewSize();
Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);
int result = scanner.scanImage(barcode);
if (result != 0) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
scanText.setText("barcode result " + sym.getData());
barcodeScanned = true;
}
}
}
};
// Mimic continuous auto-focusing
Camera.AutoFocusCallback autoFocusCB = new Camera.AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(doAutoFocus, 1000);
}
};
}
答案 0 :(得分:1)
在片段中,您必须调用getActivity()
方法来获取上下文。因为片段必须与活动相关联并且具有活动上下文(活动扩展了上下文)。
答案 1 :(得分:1)
Activity扩展了Context,但Fragments没有。这就是为什么你可以将this
传递给上下文参数的原因。因此,错误消息将您的ScanFragment视为Context的参数,该参数不起作用。每个片段都与一个Activity的片段相关联。您可以使用getActivity()
或getActivity().getApplicationContext()
来接收在这些情况下使用的有效上下文。
您的新代码将是:
mPreview = new CameraPreview(getActivity(), mCamera, previewCb, autoFocusCB);