JavaForAndroid
JavaForAndroid

Reputation: 1179

App crashes sometimes after onClick - Android

I have a curious problem. My app has a custom Dialog with a button to save a password. It works fine. But sometimes it crashes (I do the same interactions). The Logcat throws a NullPointerException. But I checked the code and did not find a part that causes this error.

The problem is that it is difficult to simulate the error. It occurs just sometimes. What might be a reason? The app hangs sometimes and if I press the button again it crashes.

This is the logcat:

java.lang.NullPointerException
at de.tapps.guardian.free.MainActivity$7$1.onClick(MainActivity.java:1312)
at android.view.View.performClick(View.java:4232)
at android.view.View$PerformClick.run(View.java:17298)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)

This seems to be the error code (line 1312ff.):

@Override
                        public void onClick(View v) {
                            dialog.cancel();
                            SharedPreferences settings = getSharedPreferences("App", 0);
                            SharedPreferences.Editor editor = settings.edit();
                            editor.putString("pass",
                                    ((EditText) findViewById(R.id.input_password)).getText().toString());
                            editor.commit();
                            setContentView(R.layout.settings);

Upvotes: 0

Views: 166

Answers (2)

Piyush
Piyush

Reputation: 18933

Change

dialog.cancel();

to

dialog.dismiss();

And find id of

EditText edt = (EditText) findViewById(R.id.input_password))

on onCreate() method:

After that get value of EditText in String

String str = edt.getText().toString();

pass this to SharedPreference

editor.putString("pass",str);

Upvotes: 2

shivamDev31
shivamDev31

Reputation: 469

Put a try / catch or if condition there...It mostly happens when you asks user to enter some input and users does not enter the input and Clicks on 'Save Password' button...If you want post that Save password code snippet here and I will help you with the same... Hope this will resolve your problem...

Thanks

Upvotes: 0

Related Questions