ViewPager在包含相机的滑动上显示黑屏

时间:2018-02-23 08:45:06

标签: android crash fragment

当Activity运行时,它会显示相机片段,但是当我向其他片段滑动视图寻呼机时,它会将屏幕显示为全黑。我无法访问其他片段的内容。

这是我的视图寻呼机的布局。

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="lifeline.learn.com.suggestions.WallOfTheApp">

        <android.support.design.widget.TabLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/tabLayout"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:animateLayoutChanges="true"
            android:background="@drawable/backgroundcorner"
            android:layout_height="match_parent"
            android:layout_below="@+id/tabLayout"/> </RelativeLayout>

以下是该活动的类

public class WallOfTheApp extends AppCompatActivity {

    ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_wall_of_the_app);
        requestPermissions (new String[]{Manifest.permission.CAMERA}, 10);
        TabLayout tabLayout = findViewById (R.id.tabLayout);
        viewPager = findViewById (R.id.viewPager);
        //Button cameraBtn = findViewById (R.id.camer_button);
        tabLayout.addTab (tabLayout.newTab ().setText ("MainFragment"));
        tabLayout.addTab (tabLayout.newTab ().setText ("PrfileInfo"));
        tabLayout.addTab (tabLayout.newTab ().setText ("Camera"));
        PagerAdapter adapter = new PageAdapter (getSupportFragmentManager (), tabLayout.getTabCount ());
        viewPager.setAdapter (adapter);
        viewPager.setOffscreenPageLimit (tabLayout.getTabCount ());
        viewPager.setCurrentItem (1);
        //viewPager.setPageTransformer (false, new PageTransformerAnimation ());
        viewPager.setOnPageChangeListener (new TabLayout.TabLayoutOnPageChangeListener (tabLayout));
        tabLayout.setOnTabSelectedListener (new TabLayout.OnTabSelectedListener () {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem (tab.getPosition ());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onBackPressed() {
        if(viewPager.getCurrentItem () != 1){
            viewPager.setCurrentItem (1);
        }else{
            super.onBackPressed ();
        }
    }
}

这是Camera.java片段

    public class Camera extends android.support.v4.app.Fragment {
        Context context;
        private android.hardware.Camera mCamera;
        private SurfaceHolders surfaceHolders;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate (R.layout.fragment_camera, container, false);
            context = getActivity ();
            if(checkCameraHardware (context)){
                try {
                    surfaceHolders = new SurfaceHolders (context,mCamera);
                    FrameLayout preview =  view.findViewById(R.id.camera_layout);
                    preview.addView(surfaceHolders);
                }catch (Exception e){
                    Toast.makeText (context, e.getMessage (), Toast.LENGTH_SHORT).show ();
                }
            }
            return view;
        }

        private boolean checkCameraHardware(Context context) {
            if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
                return true;
            } else {
                return false;
            }
        }

        public static android.hardware.Camera getCameraInstance(){
            android.hardware.Camera c = null;
            try {
                c = android.hardware.Camera.open(); // attempt to get a Camera instance
            }
            catch (Exception e){
            }
            return c;
        }

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

这是表面支架类

public class SurfaceHolders extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private android.hardware.Camera mCamera;

    public SurfaceHolders(Context context, android.hardware.Camera camera) {
        super(context);
        mCamera = camera;
        mHolder = getHolder();
        mHolder.addCallback(this);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            if(mCamera != null) {
                mCamera = Camera.open();
                mCamera.setPreviewDisplay (holder);
                mCamera.startPreview();
                mCamera.setDisplayOrientation (90);
            }
        } catch (IOException e) {

        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        if(mCamera != null){
            mCamera.stopPreview ();
            mCamera.release ();
            mCamera = null;
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

        if (mHolder.getSurface() == null){
            return;
        }
        try {
            mCamera.stopPreview();
        } catch (Exception e){

        }
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
        } catch (Exception e){
            Log.e ("Error",e.getMessage ());
        }
    }
}

这是相机片段布局

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="10dp"
        tools:context="lifeline.learn.com.suggestions.Camera">
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/camera_layout">
        </FrameLayout>
    </FrameLayout>
  

我发现这个片段在滑动时存在,但它位于a   黑色屏幕。

2 个答案:

答案 0 :(得分:0)

您可以查看以下链接:TabLayout and ViewPager

简单示例:

public class FragmentOne extends Fragment {

public FragmentOne() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_one, container, false);
}

}


public class MainActivity extends AppCompatActivity {

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

    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    // Add Fragments to adapter one by one
    adapter.addFragment(new FragmentOne(), "FRAG1");
    adapter.addFragment(new FragmentTwo(), "FRAG2");
    adapter.addFragment(new FragmentThree(), "FRAG3");
    viewPager.setAdapter(adapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);


}




class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
}

答案 1 :(得分:0)

更新

不要忘记在 Camera.java 片段中释放相机...

@Override
public void onDestroyView() {

    super.onDestroyView();

    if (mCamera!=null)
        mCamera.release();
}