2017-05-08 116 views
-1

我正在尝试编写Unity插件以提供对Native Android的getSimOperatorName()getNetworkOperatorName()函数的访问。在Android Studio中,我创建了AAR与LIB 2班,你可以看到下面......尝试在空对象引用上调用虚拟方法'getSystemService(java.lang.String)'参考

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; 
    } 
} 

二等:

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; 
    } 

错误日志:

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 
+2

的【什么是一个NullPointerException,如何解决呢?(可能的复制http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do- i-fix-it) –

+0

您是否在AndroidManifest.xml文件中添加了您的应用程序类名? '' – eugeneek

+0

我注意到你问了一个问题。这个问题解决了吗?如果是的话,只需接受修复它的解决方案。 – Programmer

回答

0

当你的My_Plugin活动已初始化且static Context context = MyApplication.getContext();已执行,您的MyApplication实例尚未创建,即尚未调用onCreate(),因此contextnull当您请求MyApplication.getContext()

您应该能够通过在getMessage()方法本身调用MyApplication.getContext()晚,例如解决这个问题:

public static String getMessage() 
{ 
    context = MyApplication.getContext(); 
    TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)); 
    // ... 
} 
0

您可以通过使用方法“getApplicationContext从你的活动范围内的应用程序上下文() ”。在这种情况下,你不需要它,所以从你的活动中你可以直接调用getSystemService()方法。

 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; 
      } 
     } 
相关问题