- Home /
How do you access a custom Java class from Unity?
Hi,
I know the question above is possible, I've seen the Unity Plugins page- however, with a .Jar created from the below code, I get
Class lookup Ljava/lang/NullPointerException; attempted while exception Ljava/lang/RuntimeException; pending
in the Logcat and the app exits.
What am I doing wrong here?
My Unity-side Javascript code is:
var jo:AndroidJavaObject= new AndroidJavaObject("co.uk.damiensturdy.UnityIntents.SBIntentPluginActivity");
jo.Call("InitIntent");
jo.Call("setAction","com.bn.sdk.shop.details");
jo.Call("putExtra","product_details_ean","2940043892355");
jo.Call("startActivity");
and my Java code is:
package co.uk.damiensturdy.UnityIntents;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
public class SBIntentPluginActivity extends Activity {
private Intent androidIntent;
public void InitIntent()
{
androidIntent = new Intent();
}
public void InitIntentUriParse(String uri) {
androidIntent=new Intent(Intent.ACTION_VIEW,Uri.parse(uri));
}
public void setAction( String action) {
androidIntent.setAction(action);
}
public void putExtra(String key,String value) {
androidIntent.putExtra(key,value);
}
public void startActivity() {
startActivity(androidIntent);
}
}
I don't know much about this topic, but this may be helpful if you haven't already read it: http://answers.unity3d.com/questions/15308/can-i-access-java-code-from-unity.html
Yep, I've read that. Although it seems it could be handy in other areas, it's not usable for Android projects. Thanks though!
Answer by HazeTI · Apr 16, 2012 at 02:00 PM
I've not done this with Javascript but SBIntentPluginActivity is a class so you should be using using AndroidJavaClass rather than AndroidJavaObject.
Interesting. I used Object thinking that "I want an instance not the class itself." - I'll try your suggestion and will tick once the fix is confirmed. Cross your fingers- Thanks!
I'm afraid this fixed the crash but the plugin still has not worked as intended. The code in the class is simply flat out ignored with no output anywhere explaining what the problem is.... :-S Any further ideas? Your answer has helped though, thanks!
Actually I'm going to tick this as correct because going by the wording in my original question, you have answered it correctly. Thanks. :-)
Hmm, Have you tried putting Debug logs in your Java classes to see if they are at least called? Also, in my case all the functions I'm calling are static and I use CallStatic. You could try creating a static test function and see if that is called?
I have done that- and they do not get called. I have not tried static functions yet, although I need to be able to launch an intent so static probably won't be what I need. There's no error in Logcat, the UNity code executes properly, but any logs in the Java code do not. It feels like it's just being ignored.
Your answer
Follow this Question
Related Questions
Now that Unity 4.1.2 broke the Android plugin examples, what do you use to learn them? 1 Answer
Problem reading sensor plugin, event won't trigger 2 Answers
Unity 3D Java plugin issues 5 Answers
UnityPlayerActivity vs. UnityPlayerNativeActivity 0 Answers
java android plugin worker thread ends up in Unity mainthread 1 Answer