我正在嘗試編寫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