我正在尝试在相机预览中使用多个按钮。该应用程序具有记录按钮和下拉菜单按钮。但是,一旦我尝试为标线添加缩放按钮,该应用就会崩溃。代码可以编译,但该应用程序将无法运行。似乎在其他任何地方都找不到有用的答案。
我一直在onViewCreated(),onClick()和onCreate()之间操纵代码行,但是我无法运行该应用程序。
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
if (null == savedInstanceState) {
getFragmentManager().beginTransaction()
.replace(R.id.container,Camera2VideoFragment.newInstance())
.commit();
}
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
mTextureView = view.findViewById(R.id.texture);
mButtonVideo = view.findViewById(R.id.video);
mZoomIn = view.findViewById(R.id.imageButton1);
mZoomOut = view.findViewById(R.id.imageButton2);
mButtonVideo.setOnClickListener(this);
mZoomIn.setOnClickListener(this);
mZoomOut.setOnClickListener(this);
view.findViewById(R.id.info).setOnClickListener(this);
view.findViewById(R.id.imageView1).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.video: {
if (mIsRecordingVideo) {
stopRecordingVideo();
} else {
startRecordingVideo();
}
break;
}
case R.id.info: {
Activity activity = getActivity();
if (null != activity) {
new AlertDialog.Builder(activity)
.setMessage(R.string.intro_message)
.setPositiveButton(android.R.string.ok, null)
.show();
}
break;
}
case R.id.imageButton1: {
Activity activity = getActivity();
if (null != activity) {
ImageView img = view.findViewById(R.id.imageView1);
float x = img.getScaleX();
float y = img.getScaleY();
img.setScaleX(x + 2);
img.setScaleY(y + 2);
}
}
}
}
代码已编译,但应用程序崩溃。 Logcat告诉我错误是通过setOnClickListeners()出现在onViewCreated()中。 findViewById()为按钮分配了null,但我不知道为什么。
我最终希望使用放大/缩小按钮来缩放视频预览,而不是使用十字线。任何帮助都会很棒。我还计划制作一个可调节亮度的搜索栏,因此对此提供帮助也很好。
答案 0 :(得分:0)
我不明白您为什么使用onViewCreated()
您可以像以下代码一样实现onCreateView(...)
:
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
if (null == savedInstanceState) {
getFragmentManager().beginTransaction()
.replace(R.id.container,Camera2VideoFragment.newInstance())
.commit();
}
getWindow().getDecorView()
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
@Override
public void onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.YOUR_LAYOUT, container, false);
mTextureView = view.findViewById(R.id.texture);
mButtonVideo = view.findViewById(R.id.video);
mZoomIn = view.findViewById(R.id.imageButton1);
mZoomOut = view.findViewById(R.id.imageButton2);
mButtonVideo.setOnClickListener(this);
mZoomIn.setOnClickListener(this);
mZoomOut.setOnClickListener(this);
view.findViewById(R.id.info).setOnClickListener(this);
view.findViewById(R.id.imageView1).setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.video: {
if (mIsRecordingVideo) {
stopRecordingVideo();
} else {
startRecordingVideo();
}
break;
}
case R.id.info: {
Activity activity = getActivity();
if (null != activity) {
new AlertDialog.Builder(activity)
.setMessage(R.string.intro_message)
.setPositiveButton(android.R.string.ok, null)
.show();
}
break;
}
case R.id.imageButton1: {
Activity activity = getActivity();
if (null != activity) {
ImageView img = view.findViewById(R.id.imageView1);
float x = img.getScaleX();
float y = img.getScaleY();
img.setScaleX(x + 2);
img.setScaleY(y + 2);
}
}
}
}