简体   繁体   English

调用Activity onResume后,ViewPager中的FrameLayout膨胀

[英]FrameLayout in ViewPager inflates after Activity onResume is called

Okay so I am using a ViewPager in my app and I would like to add a Camera Preview to the last page in the ViewPager. 好的,所以我在应用程序中使用ViewPager ,我想将Camera Preview添加到ViewPager的最后一页。 I am facing some issues because in order to start the preview I am assigning it to a FrameLayout within the last page of my ViewPager. 我遇到了一些问题,因为为了开始预览,我将其分配给ViewPager最后一页内的FrameLayout。 For some reason however the ViewPager doesn't inflate its pages until after the entire onCreate() and onResume() methods are called. 但是由于某种原因,在调用整个onCreate()和onResume()方法之后,ViewPager才会膨胀其页面。 This is problematic because when I try to find the FrameLayout by ID it returns null since that page hasn't been inflated yet. 这是有问题的,因为当我尝试通过ID查找FrameLayout时,由于该页面尚未膨胀,它返回null。 Is there anyway I can fix this? 无论如何,我可以解决这个问题吗?

Here is the code for the camerapreview. 这是camerapreview的代码。 I had an if check to see if my preview was null and it is. 我进行了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);
        }
    }

}

Here is the fragment itself: 这是片段本身:

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;
    }

}

and lastly heres the xml for the fragment 最后是此片段的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>

Okay so I remedied this problem by adding the following to my fragments class 好的,我通过将以下内容添加到我的片段类中来解决此问题

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ((Setup) getActivity()).startCamera();
}

which starts the camera after the view has been properly created and inflated. 正确创建并放大视图后,它将启动相机。 This alone causes issues when you exit the app and reenter (resume) since the onResume doesn't have the startCamera command. 由于onResume没有startCamera命令,当您退出应用程序并重新输入(恢复)时,仅这会导致问题。 To fix this I just included this block of code to my main class 为了解决这个问题,我只是在主类中包含了这段代码

@Override
protected void onResume() {
    super.onResume();
    if(preview != null){
        startCamera();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM