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
1
Question by Catlard · Mar 04, 2014 at 01:28 PM · androidjavapluginsandroidjavaobject

AndroidJavaClass. Call returns null ptr.

Hey everybody. So, I have a java class, as such:

 package com.example.test;
 
 import android.app.Activity;
 import android.os.Bundle;
 
 public class test extends Activity {
     
     public static test instance;
 
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         test.instance = this;
     }
 
     
     public String getHello() {
         return "Hello";
     }
 }

And in Unity, I have placed the .jar file in the Plugins -> Android folder. I have a c# class on the main camera, as such:

 using UnityEngine;
 using System.Collections;
 
 public class CheckSDK : MonoBehaviour {
 
     public AndroidJavaClass _nabiSDK;
     private string str = "NOTHING";
 
     void OnGUI() {
 
         if(GUI.Button(new Rect(300, 0, 100, 50), "Get mode...")) {
             AndroidJavaClass jc = new AndroidJavaClass("com.example.test.test" );
             AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("instance");
             print(jo.Call<string>("getHello"));
         }
     }
 }

UPDATE: So, I am pressing the button on the device, but I only get an exception that reads: JNI: Init'd AndroidJavaObject with null ptr! Can anyone explain what I'm not doing, and why it is required? I'm going crazy trying to figure it out!

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 benni05 · Mar 05, 2014 at 09:42 AM 0
Share

Hi again, first, just in case you haven't read through it (but I guess you have done so already):

http://docs.unity3d.com/Documentation/$$anonymous$$anual/PluginsForAndroid.html

Your java Activity should extend UnityPlayerActivity

 public class test extends UnityPlayerActivity

In the onCreate you do (you did it already)

 super.onCreate(savedInstanceState);
 test.instance = this;

For this to work you have to add the classes.jar that comes with Unity to your build path of your java project (do not put it into the Plugins directory of your Unity project where only your own jar needs to be). Look up classes.jar in the docs from above to see where it resides in the Unity directory structure.

I have checked my code and never called a string method. So, just to make sure that this is not our problem call a method that returns an int like this:

 jo.Call<int>("get$$anonymous$$yInt");

And have a method in your java class:

 public int get$$anonymous$$yInt() {
     return 123;
 }

Do you want eventually deploy to an Android device or are you planning to use some non-Android related jars? If it is the first one it should work like we have discussed.

avatar image Catlard · Mar 06, 2014 at 03:19 AM 0
Share

I am deploying to android -- thanks for the update! I'll try it out, and keep you posted. Cheers, Simon

1 Reply

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

Answer by benni05 · Mar 04, 2014 at 02:34 PM

After obtaining the Java class reference you have to get an instance reference to the actual object and then do your method call on this:

 AndroidJavaClass jc = new AndroidJavaClass("com.example.test.test" );
 AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("instance");
 str = jo.Call<string>("getHello");

This requires an instance variable in the Java class:

 public static test instance;

which would typcially be set in the onCreate method of your Java activity

 test.instance = this;

Best, Ben

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 benni05 · Mar 04, 2014 at 02:40 PM 1
Share

To avoid confusion I would recommend to name your class Test with capital T.

avatar image Catlard · Mar 04, 2014 at 02:40 PM 0
Share

Oh! Really? That's it? But what does "instance" refer to? Does it have to be "instance"? Why use that word? How do you know to use it?

You're the man,

Simon

avatar image benni05 · Mar 04, 2014 at 02:41 PM 1
Share

Please see my extended answer, I've edited it...

avatar image Catlard · Mar 04, 2014 at 02:54 PM 0
Share

Awesome. You are the man. Again.

avatar image benni05 · Mar 04, 2014 at 02:55 PM 1
Share

O$$anonymous$$, great. Thanks for accepting it as the answer.

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing Data from a Android java class 1 Answer

What is the difference between AndroidJavaClass.Call and AndroidJavaObject.Call 2 Answers

AndroidJavaObject as array for NFC access 1 Answer

How to access methods from nested static java classes 0 Answers

Problem in android java plugin 0 Answers


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