-1

I'm trying to write Unity plugin to give access to getSimOperatorName() and getNetworkOperatorName() functions of Native Android. In Android Studio I am creating an AAR lib with 2 classes, that you can see below...

package com.eppz.myplugin;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;

public class My_Plugin extends Activity
{
    static Context context = MyApplication.getContext();
    static String test = "";

    public static String getMessage()
    {
        TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));

        try {
            test = telephonyManager.getSimOperatorName();
        }catch (Exception e){
            test = e.getMessage();
        }
        return test;
    }
}

Second class:

package com.eppz.myplugin;
import android.app.Application;
import android.content.Context;

public class MyApplication extends Application {

    private static Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
    }

    public static Context getContext() {
        return mContext;
    }

Error Log:

05-08 05:23:26.245 30107-30152/? I/Unity: AndroidJavaException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
  java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
      at com.eppz.myplugin.My_Plugin.getMessage(My_Plugin.java:15)
      at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
      at com.unity3d.player.UnityPlayer.a(Unknown Source)
      at com.unity3d.player.UnityPlayer$c$1.handleMessage(Unknown Source)
      at android.os.Handler.dispatchMessage(Handler.java:98)
      at android.os.Looper.loop(Looper.java:135)
      at com.unity3d.player.UnityPlayer$c.run(Unknown Source)
      at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <filename unknown>:0 
      at UnityEngine.AndroidJNISafe.CallStaticStringMethod (IntPtr clazz, IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <filename unknown>:0 
      at UnityEngine.Androi
Floern
  • 33,559
  • 24
  • 104
  • 119
GoodSoul
  • 63
  • 3
  • 7

2 Answers2

1

When your My_Plugin activity is initialized and static Context context = MyApplication.getContext(); is executed, your MyApplication instance has not been created yet, i.e. onCreate() has not been called, thus context is null when you request it with MyApplication.getContext().

You should be able to fix this by calling MyApplication.getContext() late on, for example in the getMessage() method itself:

public static String getMessage()
{
    context = MyApplication.getContext();
    TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
    // ...
}
Floern
  • 33,559
  • 24
  • 104
  • 119
1

You can get the application context from within your activity by using the method "getApplicationContext()". In this case you don't need it, so from your activity you can call the getSystemService() method directly.

     public class My_Plugin extends Activity
        {
            static String test = "";

            public static String getMessage()
            {
                TelephonyManager telephonyManager = ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE));

                try {
                    test = telephonyManager.getSimOperatorName();
                }catch (Exception e){
                    test = e.getMessage();
                }
                return test;
            }
        }
ale.m
  • 883
  • 7
  • 14