来自OpenCV的机器人活动致电时崩溃 [英] OpenCv crashes when calling from android activity

查看:368
本文介绍了来自OpenCV的机器人活动致电时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code中,我使用OpenCV的推出相机。的code为如下。如果该类MainActivity作为主类,然后每一件事工作正常,但如果我用它从另一个活动,然后应用程序崩溃。

i have this code in which i use openCV to launch a camera. The code is given below. If this class "MainActivity" is used as the main class then every thing works fine but if i use it from another activity then the application crashes.

下面是OPENCV的code的android

Here's the code of openCV android

public class MainActivity extends Activity implements CvCameraViewListener2 {

private CameraBridgeViewBase mOpenCvCameraView;

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
               // Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this,     mLoaderCallback);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
     setContentView(R.layout.helloopencvlayout);
     mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
     mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
     mOpenCvCameraView.setCvCameraViewListener((CvCameraViewListener2) this);

}

@Override
 public void onPause()
 {
     super.onPause();
     if (mOpenCvCameraView != null)
         mOpenCvCameraView.disableView();
 }

 public void onDestroy() {
     super.onDestroy();
     if (mOpenCvCameraView != null)
         mOpenCvCameraView.disableView();
 }

 public void onCameraViewStarted(int width, int height) {
 }

 public void onCameraViewStopped() {
 }

 public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
     return inputFrame.rgba();

 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

现在,如果我尝试使用意图中,我只是有一个按钮可以从另一个活动调用此和的onclick方法的实现,当按钮pressed,这一活动被调用时,应用程序崩溃。

Now if i try to call this from another activity using intent in which i just have a button and onclick method is implemented and when the button is pressed , this activity is called , the application crashes.

它已经更多的4个小时,我与它挣扎。请帮忙在此先感谢。

It's been more the 4 hours , i am struggling with it. Please help and thanks in advance.

我所相信的是,有实现的回调方法,所以我们不能把这种活动作为正常活动通话,或者是一些其他的概念。我搜索它在互联网上,但没有什么帮助。一些地方我还发现异步调用的概念,但我没有得到它。

What i believe is that there is a callback method implemented , so we can't call this activity as normal activity call , or may be some other concept. I search it on the internet , but nothing helps. Some where i also found the concept of Async call , but i didn't get it.

请帮忙。
谢谢你。

Please help. Thanks.

推荐答案

如果您将使用试穿的OnCreate功能catch块,那么你会知道,这个问题是类型转换异常是应用程序无法在强制转换的组织.opencv活动类,您的活动。

If you will use try catch block on the oncreate function then you will come to know that the problem is of typecasting exception that is application can't typecast the org.opencv activity class to your activity.

所以,如果不是这种类型转换,如果我类型转换MainActivity.this,问题就解决了​​。

So Instead if typecasting this , if i type cast MainActivity.this , the problem is solved.

很简单却花了我的时间地狱弄清楚。

Simple but took me hell of time to figure out.

这篇关于来自OpenCV的机器人活动致电时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆