Null pointer exception when calling POSHandler.Instance in Xamarin Android project - android

I am trying to integrate following SDK into a Xamarin Android App: https://github.com/developermypos/myPOS-SDK-Android
If I try to get the POSHandler Instance I get following exception:
Java.Lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName():
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)
In the POSHandler.java file I found following function, which obviously causes the issue:
void setDefaultInfoListener()
{
final Toast toast = Toast.makeText(SDKApplication.getAppContext(), "", 1); // <-- **Exception**!
TextView textView = (TextView)toast.getView().findViewById(16908299);
if (textView != null) {
textView.setGravity(17);
}
My code:
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**
...
Any ideas what might be the issue?

Related

ViewRootImpl.handleWindowFocusChanged View.findFocus() on a null object reference

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findFocus()' on a null object reference
at android.view.ViewRootImpl.handleWindowFocusChanged(ViewRootImpl.java:3472)
at android.view.ViewRootImpl.access$1100(ViewRootImpl.java:205)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:5267)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:268)
at android.app.ActivityThread.main(ActivityThread.java:8023)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:635)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
mView.findFocus()
mView is DecorView

IWindowSession.java NullPointerException: Attempt to read from field 'int android.content.pm.ApplicationInfo.flags' on a null object reference

Fatal Exception: java.lang.NullPointerException: Attempt to read from field 'int android.content.pm.ApplicationInfo.flags' on a null object reference
at android.os.Parcel.createException(Parcel.java:1972)
at android.os.Parcel.readException(Parcel.java:1934)
at android.os.Parcel.readException(Parcel.java:1884)
at android.view.IWindowSession$Stub$Proxy.addToDisplay(IWindowSession.java:836)
at android.view.ViewRootImpl.setView(ViewRootImpl.java:926)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:390)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.widget.Toast$TN.handleShow(Toast.java:525)
at android.widget.Toast$TN$1.handleMessage(Toast.java:429)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:226)
at android.app.ActivityThread.main(ActivityThread.java:7191)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)
showing some fatal exception in IWindowSession.java line 836
android.view.IWindowSession$Stub$Proxy.addToDisplay please help to resolve it.

Calling onSaveInstanceState on Activity

I inherited a project, trying to understand and solve an old bug.
When I start an activity and let it sit for a bit (sometimes it's almost immediate), but it appears to go into a pause state or something and attempts to reload the activity and gets an error.
"Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String ... ' on a null object reference"
I found that when I add this to my activity:
#Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
//super.onSaveInstanceState(savedInstanceState);
}
It appears to work perfectly.
However, when I uncomment the line above to super.onSaveInstanceState(savedInstanceState);, I get the error again.
My activity is extending the ActionBarActivity, if that is helpful.
Any help would be appreciated. Thank you!
Related Code:
#Override
protected void prepareLayout() {
textMessage = (TextView) findViewById(R.id.textMessage);
textNotes = (EditText) findViewById(R.id.textNotes);
textMessage.setText(getOneOfMyItems().getText());
// ... the above line is where the problem error is thrown.
}
Stack Trace:
Process: com.myproj.myproj, PID: 9272
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproj.myproj/com.myproj.activities.MyActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.myproj.data.MyItems.getText()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3384)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5433)
at android.app.ActivityThread.access$1200(ActivityThread.java:226)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1810)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7270)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.myproj.data.MyItems.getText()' on a null object reference
at com.myproj.xyz.MyFragment.prepareLayout(MyFragment.java:44)
at com.myproj.fragments.BaseFragment.prepareAndSetView(BaseFragment.java:49)
at at com.myproj.xyz.BaseMyFragment.onCreateView(BaseMyFragment.java:21)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2143)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2089)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1129)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2143)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:615)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1266)
at android.app.Activity.performStart(Activity.java:6943)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3384) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5433) 
at android.app.ActivityThread.access$1200(ActivityThread.java:226) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1810) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7270) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Unable to start Acitivity on miui9

I'm new to Android and currently I have a thorny problem.
My application is crashed on MIUI9(Android 7.1.1), but it's working good on other device(Android N). I don't know how did it happend.
Below is the log:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.*****.***.***/com.*****.***.***.SplashActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Thread android.os.Looper.getThread()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2740)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2801)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1548)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6365)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:791)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Thread android.os.Looper.getThread()' on a null object reference
at miui.util.AttributeResolver.bR(SourceFile:35)
at miui.util.AttributeResolver.resolveInt(SourceFile:145)
at miui.util.AttributeResolver.isUsingMiuiTheme(SourceFile:179)
at miui.os.Environment.isUsingMiui(SourceFile:108)
at android.view.ViewConfigurationInjector.get(ViewConfigurationInjector.java:18)
at android.view.ViewConfiguration.get(ViewConfiguration.java:369)
at android.view.View.<init>(View.java:4028)
at android.view.View.<init>(View.java:4154)
at android.view.ViewGroup.<init>(ViewGroup.java:579)
at android.view.ViewGroup.<init>(ViewGroup.java:575)
at android.view.ViewGroup.<init>(ViewGroup.java:571)
at android.view.ViewGroup.<init>(ViewGroup.java:567)
at android.widget.FrameLayout.<init>(FrameLayout.java:78)
at com.android.internal.policy.DecorView.<init>(DecorView.java:237)
at com.android.internal.policy.PhoneWindow.generateDecor(PhoneWindow.java:2283)
at com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:2621)
at com.android.internal.policy.PhoneWindow.getDecorView(PhoneWindow.java:2049)
at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:371)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:320)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:272)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:148)
at cn.droidlover.xdroidmvp.mvp.XActivity.onCreate(XActivity.java:42)
at com.*****.***.***.BaseActivity.onCreate(BaseActivity.java:48)
at com.*****.***.***.SplashActivity.onCreate(SplashActivity.java:49)
at android.app.Activity.performCreate(Activity.java:6861)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)

onPause() crashes when calling mediaplayer.isPlaying()

Okay so I got two audio tracks in the same activity and hence am using two media players for them. I've got everything working fine. The only problem is when calling the onPause() so that the audio track gets paused when activity pauses.
If audio track 1 is playing then on pausing the app crashes because of a null pointer exception due to mediaPlayer2 and the opposite happens when track 2 is playing.
Here's the code from the Activity.java
#Override
protected void onPause() {
super.onPause();
if (mediaPlayer1.isPlaying()) {
mediaPlayer1.pause();
pp1.setImageResource(R.drawable.play);
}
if (mediaPlayer2.isPlaying()) {
mediaPlayer2.pause();
pp2.setImageResource(R.drawable.play);
}
}
And the error(s)
Error 1
java.lang.RuntimeException: Unable to pause activity {com.music.track/com.music.track.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3454)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3408)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3383)
at android.app.ActivityThread.access$1100(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference
at com.music.track.MainActivity.onPause(MainActivity.java:186)
at android.app.Activity.performPause(Activity.java:6397)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1380)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3435)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3408) 
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3383) 
at android.app.ActivityThread.access$1100(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:152) 
at android.app.ActivityThread.main(ActivityThread.java:5497)
Error 2
java.lang.RuntimeException: Unable to pause activity {com.music.test/com.music.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3454)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3408)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3383)
at android.app.ActivityThread.access$1100(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference
at com.music.test.MainActivity.onPause(MainActivity.java:181)
at android.app.Activity.performPause(Activity.java:6397)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1380)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3435)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3408) 
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3383) 
at android.app.ActivityThread.access$1100(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:152) 
at android.app.ActivityThread.main(ActivityThread.java:5497)
Check mediaPlayer1 & 2 for null before using them

Categories

Resources