java - NullPointerException when dialog positive button onClick calls interface -
i'm trying use interface send click event alertdialog.builder.setpositivebutton's onclick event.
please me out. i've been looking on similar problem use guidance.
the npe @ line:
mlistener.onsetbalancedialogpositiveclick(setbalancedialog.this);
here logcat:
08-19 20:05:50.469: e/androidruntime(5795): fatal exception: main 08-19 20:05:50.469: e/androidruntime(5795): java.lang.nullpointerexception 08-19 20:05:50.469: e/androidruntime(5795): @ com.smb.allowance.setbalancedialog$2.onclick(setbalancedialog.java:75) 08-19 20:05:50.469: e/androidruntime(5795): @ com.android.internal.app.alertcontroller$buttonhandler.handlemessage(alertcontroller.java:159) 08-19 20:05:50.469: e/androidruntime(5795): @ android.os.handler.dispatchmessage(handler.java:99) 08-19 20:05:50.469: e/androidruntime(5795): @ android.os.looper.loop(looper.java:123) 08-19 20:05:50.469: e/androidruntime(5795): @ android.app.activitythread.main(activitythread.java:3683) 08-19 20:05:50.469: e/androidruntime(5795): @ java.lang.reflect.method.invokenative(native method) 08-19 20:05:50.469: e/androidruntime(5795): @ java.lang.reflect.method.invoke(method.java:507) 08-19 20:05:50.469: e/androidruntime(5795): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:864) 08-19 20:05:50.469: e/androidruntime(5795): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:622) 08-19 20:05:50.469: e/androidruntime(5795): @ dalvik.system.nativestart.main(native method)
here dialog code:
package com.smb.allowance; import java.text.decimalformat; import android.app.activity; import android.app.alertdialog; import android.app.dialog; import android.content.context; import android.content.dialoginterface; import android.os.bundle; import android.support.v4.app.dialogfragment; import android.util.log; import android.view.keyevent; import android.view.view; import android.view.inputmethod.editorinfo; import android.view.inputmethod.inputmethodmanager; import android.widget.edittext; import android.widget.textview; import android.widget.textview.oneditoractionlistener; import android.widget.toast; public class setbalancedialog extends dialogfragment implements oneditoractionlistener { private edittext edittextsetbalance; private static double dinput = 0; public interface setbalancedialoglistener{ public void onsetbalancedialogpositiveclick(dialogfragment dialog); } // end interface setbalancedialoglistener mlistener; public void onattache(activity activity){ super.onattach(activity); try { mlistener = (setbalancedialoglistener) activity; }catch (classcastexception e) { throw new classcastexception(activity.tostring() + " must implement setbalancedialoglistener"); } } // end onattach public setbalancedialog(){ // default empty constructor }// end constructor public dialog oncreatedialog(bundle savedinstancestate){ view view = getactivity().getlayoutinflater().inflate(r.layout.dialog_balance_set, null); edittextsetbalance = (edittext)view.findviewbyid(r.id.edittextsetbalance); edittextsetbalance.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (!edittextisempty(edittextsetbalance)){ edittextsetbalance.settext(""); } clearedittext(edittextsetbalance); } }); alertdialog.builder builder = new alertdialog.builder(getactivity()); builder.setview(view) .setcancelable(false) .setmessage(r.string.set_begin_balance) .setcancelable(false) .setpositivebutton(r.string.save, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub mlistener.onsetbalancedialogpositiveclick(setbalancedialog.this); } }); // show soft keyboard automatically edittextsetbalance.requestfocus(); edittextsetbalance.setoneditoractionlistener(this); return builder.create(); } // end oncreatedialog
the activity implements interface:
package com.smb.allowance; import java.text.decimalformat; import android.content.context; import android.content.intent; import android.os.bundle; import android.support.v4.app.dialogfragment; import android.support.v4.app.fragmentactivity; import android.util.log; import android.view.keyevent; import android.view.menu; import android.view.view; import android.view.inputmethod.editorinfo; import android.view.inputmethod.inputmethodmanager; import android.widget.button; import android.widget.edittext; import android.widget.spinner; import android.widget.textview; import android.widget.textview.oneditoractionlistener; import android.widget.toast; import com.smb.allowance.resetbalancedialogfragment.resetbalancedialoglistener; public class mainactivity extends fragmentactivity implements resetbalancedialoglistener, setbalancedialog.setbalancedialoglistener { @override public void onsetbalancedialogpositiveclick(dialogfragment dialog) { // testing functionality onreset(); }
is possible how handling/mishandling edittext view causing npe?
i'm new android/java , programming in general, help.
mlistener null
it null because never assign anything! (oh yes hear think!) don't
you have public void
onattache(activity activity)` method never called because should be
public void onattach(activity activity)
you should add @override inherited methods , call super. make sure compilation error if method declaration wrong ensuring code in base class inherited method gets run. if method empty in version of sdk using doesn't mean won't empty @ unkown point in future.
Comments
Post a Comment