Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DamienSturdy · Apr 15, 2012 at 08:44 PM · androidpluginjava

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);
         
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lttldude · Apr 15, 2012 at 09:07 PM 1
Share

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

avatar image DamienSturdy · Apr 15, 2012 at 10:29 PM 0
Share

Yep, I've read that. Although it seems it could be handy in other areas, it's not usable for Android projects. Thanks though!

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DamienSturdy · Apr 16, 2012 at 03:48 PM 0
Share

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!

avatar image DamienSturdy · Apr 16, 2012 at 09:33 PM 0
Share

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!

avatar image DamienSturdy · Apr 16, 2012 at 10:16 PM 0
Share

Actually I'm going to tick this as correct because going by the wording in my original question, you have answered it correctly. Thanks. :-)

avatar image HazeTI · Apr 17, 2012 at 08:56 AM 0
Share

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?

avatar image DamienSturdy · Apr 17, 2012 at 09:23 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges