2017-05-08 149 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; 
      } 
     } 
相關問題