1
votes

I've tested this multiple times on an example project / plugin, and it seems to work perfectly. Once I begin integration on my actual project, I get an error (seen below). I've lost about two weeks trying to get Unity / Android plugins working, and this is the furthest I've gotten. I feel so close, but so far from finding the solution! Any help I can get is greatly appreciated. Thank you!

The error that I'm getting:

08-28 15:38:25.200: I/Unity(6191): AndroidJavaException: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

C# (Unity):

// Update is called once per frame
void Update () {
    testPlugin ();
    printTestobj();
}

public void testPlugin() {

    if (testobj == null) {
        // First, obtain the current activity context
        using (var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
            playerActivityContext = actClass.GetStatic<AndroidJavaObject> ("currentActivity");
        }

        // Pass the context to a newly instantiated TestUnityProxy object
        using (var pluginClass = new AndroidJavaClass("com.company.product.Main")) {
            if (pluginClass != null) {
                testobj = pluginClass.CallStatic<AndroidJavaObject> ("instance");
                testobj.Call ("setContext", playerActivityContext);
            }
        }
    }
}

Java (Android):

public Main() {
    INSTANCE = this;
}

public static Main instance() {
    Log.i(TAG, "Inside instance.");
    if (INSTANCE == null) {
        Log.i(TAG, "instance b1.");
        INSTANCE = new Main();
        Log.i(TAG, "instance b2.");
    }
    Log.i(TAG, "instance b3.");
    return INSTANCE;
}

public void setContext(Context ctx) {
    this.context = ctx;
    Log.i(TAG, "Application context set.");
}
1
You should show us your entire stack trace. Just call Looper.prepare() at the beginning of the thread's job (Runnable, AsyncTask, etc). - mstrthealias

1 Answers

-1
votes

I don't know if this has been answered, but I'm adding this comment to help keep this issue alive.

The first step is to figure out what line of code the issue exists on. And you don't know that because Unity won't tell you.

I am also developing Android plugins for Unity. One of the issues I'm seeing is that RuntimeException are caught by the Unity layer and the natural stacktrace seems to be replaced by something that Unity wants to insert.

Until Unit starts showing stacktraces instead of catching them and displaying something different, these kinds of things are going to be impossible to solve. After all how can you solve a bug if you have to guess what line of code an error occurred on?