gpt4 book ai didi

java - 如何正确子类 SimpleCameraHost 以在 caac-camera 中使用?

转载 作者:行者123 更新时间:2023-12-02 04:38:09 25 4
gpt4 key购买 nike

这是我在尝试当前方法时遇到的完整错误。

05-28 09:33:13.022    8163-8163/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.snappiesticker.cwac4, PID: 8163
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.snappiesticker.cwac4/com.snappiesticker.cwac4.MainActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class com.commonsware.cwac.camera.CameraView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class com.commonsware.cwac.camera.CameraView
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.snappiesticker.cwac4.DemoCameraFragment.onCreateView(DemoCameraFragment.java:28)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:834)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.Activity.performStart(Activity.java:6005)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.snappiesticker.cwac4.DemoCameraFragment.onCreateView(DemoCameraFragment.java:28)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:834)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.Activity.performStart(Activity.java:6005)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalArgumentException: To use the two- or three-parameter constructors on CameraView, your activity needs to implement the CameraHostProvider interface
at com.commonsware.cwac.camera.CameraView.<init>(CameraView.java:74)
at com.commonsware.cwac.camera.CameraView.<init>(CameraView.java:62)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.snappiesticker.cwac4.DemoCameraFragment.onCreateView(DemoCameraFragment.java:28)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:834)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.Activity.performStart(Activity.java:6005)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我相信这是因为我在此处的 fragment 中使用了错误的上下文:

public class DemoCameraFragment extends CameraFragment implements View.OnClickListener {
//private CapturedImage next = null;

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View content=inflater.inflate(R.layout.camera, container, false);
CameraView cameraView=(CameraView)content.findViewById(R.id.camera);

setCameraView(cameraView);

return(content);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHost(new DemoCameraHost(getActivity()));
}

@Override
public void onStart() {
super.onStart();

getView().setOnClickListener(this);
}
@Override

public void onClick(View v) {
takePicture();
Toast.makeText(getActivity(), "picture taken!",
Toast.LENGTH_LONG).show();
}
public class DemoCameraHost extends SimpleCameraHost {
public DemoCameraHost(Context _ctxt) {
super(_ctxt);
}

@Override
public boolean useFrontFacingCamera() {
return true;
}
/*
@Override
public void onCameraFail(CameraHost.FailureReason reason) {
super.onCameraFail(reason);

Toast.makeText(getActivity(),
"Sorry, but you cannot use the camera now!",
Toast.LENGTH_LONG).show();
}

@Override
public void saveImage(PictureTransaction xact, Bitmap bitmap) {
super.saveImage(xact, bitmap);

next = new CapturedImage();
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, next).addToBackStack(null).commit();
}*/
}
}

我认为需要工作的行是 setHost(new DemoCameraHost(getActivity())); 我使用 getActivity 假设相机 fragment 的上下文相当于它的主要 Activity 的上下文住在但我不确定

我想做的就是子类化SimpleCameraHost,以便我可以重写方法而不是使用默认值。

但是,它还提到了主要 Activity 的二进制 xml 问题,所以问题可能就在这里

这是MainActivity.java:

public class MainActivity extends Activity {
private DemoCameraFragment current = null;

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

setContentView(R.layout.activity_main);

current = new DemoCameraFragment();

getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, current).commit();
}
}

与activity_main.xml一起

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

最佳答案

引用异常:

Caused by: java.lang.IllegalArgumentException: To use the two- or three-parameter constructors on CameraView, your activity needs to implement the CameraHostProvider interface

您的 Activity 未实现 CameraHostProvider 接口(interface)。请参阅 the documentation 中的“在布局资源中使用 CameraView” (我很想直接链接到该部分,但 GitHub 和我的浏览器目前无法相处......)。

关于java - 如何正确子类 SimpleCameraHost 以在 caac-camera 中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30513742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com