好的,我在我的应用程序中使用ViewPager,我想在ViewPager的最后一页添加一个Camera Preview。我面临一些问题,因为为了开始预览,我将它分配给我的ViewPager的最后一页内的FrameLayout。但是出于某种原因,在调用整个onCreate()和onResume()方法之前,ViewPager不会对其页面进行充气。这是有问题的,因为当我尝试通过ID找到FrameLayout时,它返回null,因为该页面尚未膨胀。无论如何我可以解决这个问题吗?
以下是camerapreview的代码。我有一个if检查,看我的预览是否为空,它是。
package me.bionicsheep.aegis;
import com.qualcomm.snapdragon.sdk.face.FacialProcessing;
import android.os.Bundle;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PreviewCallback;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout.LayoutParams;
public class Setup extends FragmentActivity implements Camera.PreviewCallback{
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
CameraActivationFragment CAF;
Camera cameraObj;
FrameLayout preview;
private CameraSurfacePreview mPreview;
private int FRONT_CAMERA_INDEX = 1;
private boolean qCommHardware;
public static FacialProcessing facialProc;
LayoutParams mParams;
Parameters cParams;
int cWidth;
int cHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup_layout);
hideActionBar();
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
checkForHardware();
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
CAF = new CameraActivationFragment();
return CAF;
case 1:
return new ScreenSlidePageFragment();
}
return null;
}
@Override
public int getCount() {
return 2;
}
}
private void hideActionBar(){
getActionBar().hide();
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// TODO Auto-generated method stub
}
private void checkForHardware(){
qCommHardware = FacialProcessing.isFeatureSupported(FacialProcessing.
FEATURE_LIST.FEATURE_FACIAL_RECOGNITION);
if(qCommHardware){
facialProc = (FacialProcessing) FacialProcessing.getInstance();
facialProc.setRecognitionConfidence(85);
}else{
finish();
}
}
public void startCamera(){
try{
cameraObj = Camera.open(FRONT_CAMERA_INDEX);
}
catch (Exception ex){
finish();
Log.d("Aegis", "failed to open front facing camera " + ex.getMessage());
}
setRotation();
mPreview = new CameraSurfacePreview(Setup.this, cameraObj);
preview = (FrameLayout) findViewById(R.id.camera_activation_preview);
if(preview == null){
Log.d("Aegis","preview is null");
return;
}
cParams = cameraObj.getParameters();
cWidth = cParams.getPictureSize().width;
cHeight = cParams.getPictureSize().height;
mParams = (LayoutParams) preview.getLayoutParams();
mParams.width = (int) (getResources().getDisplayMetrics().widthPixels / 1.5);
mParams.height = (int) (mParams.width * ((double) cWidth / cHeight));
preview.addView(mPreview, mParams);
cameraObj.setPreviewCallback((PreviewCallback) Setup.this);
}
@Override
protected void onPause() {
super.onPause();
preview.removeView(mPreview);
}
@Override
protected void onResume() {
super.onResume();
//startCamera();
}
private void setRotation(){
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0)
{
cameraObj.setDisplayOrientation(90);
}
if(display.getRotation() == Surface.ROTATION_270)
{
cameraObj.setDisplayOrientation(180);
}
}
}
这是片段本身:
package me.bionicsheep.aegis;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CameraActivationFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.camera_activation_layout, container, false);
return rootView;
}
}
最后是片段的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/camera_activation_preview"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</FrameLayout>
</RelativeLayout>
答案 0 :(得分:0)
好的,我通过将以下内容添加到我的片段类
来解决这个问题@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((Setup) getActivity()).startCamera();
}
在视图正确创建并充气后启动相机。退出应用程序并重新进入(恢复)时,这就会导致问题,因为onResume没有启动startCamera命令。为了解决这个问题,我将这段代码包含在我的主类
中@Override
protected void onResume() {
super.onResume();
if(preview != null){
startCamera();
}
}