- Home /
The question is answered, right answer was accepted
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
}
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.
Follow this Question
Related Questions
AndroidJavaClass. Call returns null ptr. 1 Answer
AndroidJavaObject.Call fails with 'method not found' 1 Answer
What is the difference between AndroidJavaClass.Call and AndroidJavaObject.Call 2 Answers
How to remove an add? 0 Answers
Callback from native Java to Unity's C# script has some weird (latency?) issue 0 Answers