Thormgrim Thormgrim - 2 months ago 18
Android Question

Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference

I want to run some OpenCv Filters over images inside of an Android App.
I've hit an Error, of which I cant seem to find the origin.

Stacktrace:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:222)
at android.app.AlertDialog$Builder.<init>(AlertDialog.java:452)
at org.opencv.android.BaseLoaderCallback.onPackageInstall(BaseLoaderCallback.java:90)
at org.opencv.android.AsyncServiceHelper.InstallService(AsyncServiceHelper.java:117)
at org.opencv.android.AsyncServiceHelper.initOpenCV(AsyncServiceHelper.java:33)
at org.opencv.android.OpenCVLoader.initAsync(OpenCVLoader.java:100)
at project.fragment.OpenCvTestFragment.onCreateView(OpenCvTestFragment.java:94)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


My Code: ( OpenCvTestFragment.java )

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgcodecs.Imgcodecs;

import java.io.IOException;

import team_excellence.growup.R;

public class OpenCvTestFragment extends Fragment implements CameraBridgeViewBase.CvCameraViewListener2, View.OnTouchListener {

private OnOpenCvTestInteractionListener mListener;
private View view;
private ImageView imgView1;
private ImageView imgView2;

public OpenCvTestFragment() {

}

public static OpenCvTestFragment newInstance(String param1, String param2) {
OpenCvTestFragment fragment = new OpenCvTestFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}

private static final String TAG = "OCVSample::Activity";

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
view = inflater.inflate(R.layout.fragment_open_cv_test, container, false);

final ImageView imgView1 = (ImageView) view.findViewById(R.id.ocvImage1);
imgView1.setImageResource(R.drawable.img1);

ImageView imgView2 = (ImageView) view.findViewById(R.id.ocvImage2);
imgView1.setImageResource(R.drawable.img2);

final Button contoursButton = (Button) view.findViewById(R.id.contoursButton);
contoursButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {

try {
applyContoursFilter("img1");
} catch (IOException e) {
e.printStackTrace();
}

}
});
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_8, getContext(),
mOpenCVCallBack);
return view;
}

public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onOpenCvTestInteraction(uri);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnOpenCvTestInteractionListener) {
mListener = (OnOpenCvTestInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnOpenCvTestInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

@Override
public void onCameraViewStarted(int width, int height) {

}

@Override
public void onCameraViewStopped() {

}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
return null;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}

public interface OnOpenCvTestInteractionListener {
void onOpenCvTestInteraction(Uri uri);
}

public void applyContoursFilter(String resName) throws IOException {

Mat matImg = convertImageToMat(resName);
Mat matResult = findContours(matImg);
Bitmap bm = Bitmap.createBitmap(matResult.cols(), matResult.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(matResult, bm);
imgView1.setImageBitmap(bm);

}

private Mat convertImageToMat(String resName) throws IOException {
int drawableResourceId = this.getResources().getIdentifier(resName, "drawable", getActivity().getPackageName());
Mat img = Utils.loadResource(getContext() , drawableResourceId , Imgcodecs.CV_LOAD_IMAGE_COLOR);

return img;
}


private Mat findContours (Mat image){

Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(19,19));
Mat closed = new Mat(); // closed will have type CV_32F
Imgproc.morphologyEx(image, closed, Imgproc.MORPH_CLOSE, kernel);
Core.divide(image, closed, closed, 1, CvType.CV_32F);
Core.normalize(closed, image, 0, 255, Core.NORM_MINMAX, CvType.CV_8U);
Imgproc.threshold(image, image, -1, 255, Imgproc.THRESH_BINARY_INV
+ Imgproc.THRESH_OTSU);

return closed;

}



}


For clarification when the corresponding Button is pressed, the "applyContoursFilter" Method is called, which then converts the image from drawables ( shown in imgView1 ), into a Mat object.
After that the "findContours" Method is called and the Mat-object then converted back into bmp format and set into the imgView1 again.

Well that's how it should work. But the nullpointer error posted above gets thrown and I cant figure out where its coming from or how to fix it.

I'm relatively new to Android and would be grateful for your Help.

Answer Source

I got it solved now, by initializing OpenCV in the MainActivity of my App and only calling it in the Fragments. I moved the BaseLoaderCallBack Code from the Fragment's OnCreateView , to the MainActivity's OnCreate. Seems to be working fine now.