opencv onCameraFrame尝试调用TextView.setText()

时间:2019-04-09 14:54:50

标签: android multithreading textview opencv4android android-runonuithread

当我尝试在onCameraFrame内使用setText()时,会遇到此崩溃:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 

基本上,我在这里描述了相同的问题: Android & OpenCV - App crashes on UI change

但建议的解决方案似乎无效。

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    frame=inputFrame.rgba();
    int match_method = Imgproc.TM_CCOEFF;

    // / result matrix
    int result_cols = frame.cols() - templ.cols() + 1;
    int result_rows = frame.rows() - templ.rows() + 1;
    Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

    // / Matching
    Imgproc.matchTemplate(frame, frame, result, match_method);

    Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    Core.MinMaxLocResult mmr = Core.minMaxLoc(result);

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            myTextElement.setText("aa");
        }
    });

1 个答案:

答案 0 :(得分:0)

解决了问题,包括在run()中的findViewByID,否则返回null。并且setText()引发异常。