在Xamarin Android项目中调用POSHandler.Instance时出现空指针异常

时间:2017-11-11 21:00:54

标签: android xamarin

我正在尝试将以下SDK集成到Xamarin Android应用中:https://github.com/developermypos/myPOS-SDK-Android

如果我尝试获取POSHandler实例,我会遇到以下异常: Java.Lang.NullPointerException:尝试调用虚方法#java; .lang.String:。

Unhandled Exception:
Java.Lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
  --- End of managed Java.Lang.NullPointerException stack trace ---
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
    at android.widget.Toast.<init>(Toast.java:103)
    at android.widget.Toast.makeText(Toast.java:256)
    at eu.leupau.icardpossdk.POSHandler.setDefaultInfoListener(POSHandler.java:149)
    at eu.leupau.icardpossdk.POSHandler.<init>(POSHandler.java:145)
    at eu.leupau.icardpossdk.POSHandler.getInstance(POSHandler.java:127)
    at md5b2d2fb169710eaa19162eaff312d3328.MainActivity.n_onCreate(Native Method)
    at md5b2d2fb169710eaa19162eaff312d3328.MainActivity.onCreate(MainActivity.java:29)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

在POSHandler.java文件中,我发现了以下功能,这显然会导致问题:

void setDefaultInfoListener()
{
final Toast toast = Toast.makeText(SDKApplication.getAppContext(), "", 1); // <-- **Exception**!
TextView textView = (TextView)toast.getView().findViewById(16908299);
if (textView != null) {
  textView.setGravity(17);
}

我的代码:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    private POSHandler _myPosHandler;

    protected override void OnCreate ( Bundle bundle ) {
        POSHandler.SetLanguage(Language.English);
        POSHandler.SetDefaultReceiptConfig(POSHandler.ReceiptPrintOnlyMerchantCopy);
        POSHandler.SetCurrency(Currency.Chf);
        POSHandler.ConnectionType = ConnectionType.Bluetooth;
        _myPosHandler = POSHandler.Instance; // <-- **EXCEPTION**
...

任何想法可能是什么问题?

0 个答案:

没有答案