在我的TextureView
中有一个Fragment
,片段必须重新膨胀布局。我在Camera
中使用Activity
API(PS:Camera API必须在Activity中调用),我可以使用SurfaceTexture
来获取Camera Preview。
质疑如何使TextureView可以获得相机预览。我厌倦了许多解决方案,但他们都得到了空对象引用。
public class MyFragment extends PresentationFragment{
private TextureView textureView;
private TextView textView;
public View onCreateView(LayoutInflater layoutInflater, @Nullable ViewGroup contatiner, Bundle savedInstanceState) {
rootView = layoutInflater.inflate(R.layout.external_display, contatiner, false);
textureView = rootView.findViewById(R.id.camera_view);
textView = rootView.findViewById(R.id.test_text);
return rootView;
}
}
在活动中:
private MyFragment myFragment;
private TextureView textureView;
private SurfaceTexture surfaceTexture;
在onCreate:
myFragment =(myFragment)getFragmentManager().findFragmentById(R.id.external_display);
View view = myFragment.getView();
textureView = view.findViewById(R.id.camera_preview);
surfaceTexture = textureView.getSurfaceTexture();
getView()null对象引用。
那是你的Activity生命周期。片段视图在onCreate中不可用。幸运的是,这应该不是问题:在onRestore之前初始化相机是不正确的。