Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Dec 26, 2014 at 01:30 AM by monogon for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by monogon · Dec 26, 2014 at 01:12 AM · javaandroidjavaobjectjninon-static

Call non-static method (from subclass) in Java from Unity

Hello!

I know there are tons of threads here regarding this topic... I've read them all... I have been trying this for 4 hours straight and my nerves are raw right now.

Here is what I want to do:

I have a class myProductSettings extended from Activity in Java which contains another class PrefsFragment extended from PreferenceFragment. In this class (PrefsFragment) I want to call a non-static method from Unity to send over some preferences (CharSequence[] eventually).

I've tried it in many different ways but I am always getting "JNI: Init'd AndroidJavaObject with null ptr!". And later "JNI: Unable to find method id for 'ReceiveCallFromUnity'".

The static call to myProductSettings works fine so far .... But I was not able to call any non-static method.

How can I get a reference to the instance of PrefsFragment (mPrefsFragment) and call a non-static function on it from Unity?

Any help is highly appreciated!!! Thanks!

Here is my setup in Java:

 public class myProductSettings extends Activity  {
     
     public static PrefsFragment mPrefsFragment;
      
     @Override
     protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);       
 
                 // Display the fragment as the main content.
                 FragmentManager mFragmentManager = getFragmentManager();
                 FragmentTransaction mFragmentTransaction = mFragmentManager
                                         .beginTransaction();
                 mPrefsFragment = new PrefsFragment();
                 mFragmentTransaction.replace(android.R.id.content, mPrefsFragment);
                 mFragmentTransaction.commit();
                
      
     }

 public static void testMessage() {
         
         Log.i(myProduct.TAG, "testMessage called from Unity to myProductSettings");
         
     }
     
 
     public class PrefsFragment extends PreferenceFragment 
     {

         
     
         @Override
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);

 [......]
             
         }
         
         
         public void ReceiveSceneNamesFromUnity(CharSequence[] sceneNames) {
             
             Log.i(myProduct.TAG, sceneNames[0].toString());
             
         }
         
         public void ReceiveCallFromUnity() {
             
             Log.i(myProduct.TAG, "Non-Static call from Unity to PrefsFragment");
             
         }
     
     }
 }




And here my setup in Unity (C#)

     void Start () {
         
         StartCoroutine("TestSend");
     }
     
     IEnumerator TestSend(){
         
         yield return new WaitForSeconds(5);
         
         SendSceneNamesToJava();
     
     }
         
     private void SendSceneNamesToJava() {
         
 #if UNITY_ANDROID && !UNITY_EDITOR
         
         AndroidJavaClass jc;
         AndroidJavaObject currentActivity, myProductSettings, jo;
         
         jc = new AndroidJavaClass("com.test.test.myProductSettings");
           jo = jc.GetStatic<AndroidJavaObject>("mPrefsFragment");
         
         jo.Call("ReceiveCallFromUnity");
         
         
 #endif

 }








Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by monogon · Dec 26, 2014 at 01:30 AM

Sorry I got it now ... mPrefsFragment was not created yet when I tried to access it.

Comment
Add comment · Show 1 · 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 aadithyabk · Jun 23, 2015 at 07:56 PM 0
Share

how did you solve it ?

Follow this Question

Answers Answers and Comments

26 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 avatar image avatar image avatar image

Related Questions

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

AndroidJavaClass. Call returns null ptr. 1 Answer

AndroidJavaObject.Call fails with 'method not found' 1 Answer

Accessing Data from a Android java class 1 Answer

Cannot create null java.lang.Long object 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