我正在尝试编写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
的【什么是一个NullPointerException,如何解决呢?(可能的复制http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do- i-fix-it) –
您是否在AndroidManifest.xml文件中添加了您的应用程序类名? '' –
eugeneek
我注意到你问了一个问题。这个问题解决了吗?如果是的话,只需接受修复它的解决方案。 – Programmer