Opencv停止在Android 6.0上工作

时间:2015-11-18 12:44:41

标签: android opencv android-context

我在我的应用程序上使用Opencv,它在Android 4.3上运行良好,但它甚至不能用Android 6.0打开。

我在SO上寻找解决方案,并找到了一个说设置包名称的解决方案。所以我做到了。

关注代码:

public static boolean initOpenCV(String Version, final Context AppContext,
        final LoaderCallbackInterface Callback)
{
    AsyncServiceHelper helper = new AsyncServiceHelper(Version, AppContext, Callback);
    Intent intent = new Intent("org.opencv.engine.BIND");
    intent.setPackage("org.opencv.engine");
    if (AppContext.bindService(intent,
            helper.mServiceConnection, Context.BIND_AUTO_CREATE))
    {
        return true;
    }
    else
    {
        AppContext.unbindService(helper.mServiceConnection);
        InstallService(AppContext, Callback);
        return false;
    }
}

但现在我收到以下错误:

11-18 10:14:43.447 24901-24930/br.com.ibramed.dermos E/AndroidRuntime:      android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
11-18 10:14:43.447 24901-24930/br.com.ibramed.dermos E/AndroidRuntime:     at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
11-18 10:14:43.447 24901-24930/br.com.ibramed.dermos E/AndroidRuntime:     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
11-18 10:14:43.447 24901-24930/br.com.ibramed.dermos E/AndroidRuntime:     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
11-18 10:14:43.447 24901-24930/br.com.ibramed.dermos E/AndroidRuntime:     at android.app.Dialog.show(Dialog.java:319)

在以下代码中:

case InstallCallbackInterface.NEW_INSTALLATION:
        {
            Looper.prepare();
            Log.d("Debug", ""+mAppContext);
            AlertDialog InstallMessage = new AlertDialog.Builder(mAppContext).create();
            InstallMessage.setTitle("Package not found");
            InstallMessage.setMessage(callback.getPackageName() + " package was not found! Try to install it?");
            InstallMessage.setCancelable(false); // This blocks the 'BACK' button
            InstallMessage.setButton(AlertDialog.BUTTON_POSITIVE, "Yes", new OnClickListener()
            {
                public void onClick(DialogInterface dialog, int which)
                {
                    callback.install();
                }
            });

            InstallMessage.setButton(AlertDialog.BUTTON_NEGATIVE, "No", new OnClickListener() {

                public void onClick(DialogInterface dialog, int which)
                {
                    callback.cancel();
                }
            });

            InstallMessage.show();
        } break;

我搜索并发现有些人说要跟踪主要的Context,因为Threads没有上下文。但我的应用程序已经这样做并使用主要上下文来创建AlertDialogs,它仍然无法正常工作。

错误是应用程序显示AlertDialog时的错误。

我真的不知道还能做什么。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

据我所知,在服务或Aplication类中你无法获得View for android 6.0的上下文,所以当OpenCv尝试显示AlertDialog时,它不知道哪个活动绑定对话框崩溃了。 在使用之前尝试在Activity onCreate方法中初始化OpenCv。

我在MainActivity中的工作init方法(kotlin上的代码):

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, object : BaseLoaderCallback(this@HistoryActivity) {
                override fun onManagerConnected(status: Int) {
                    if (LoaderCallbackInterface.SUCCESS == status)
                        Log.i(TAG, "OpenCV was loaded")
                    else
                        super.onManagerConnected(status)
                }
            })
    }