android - App crashing while trying to display a Toast on AlertDialog button click (in a Fragment) -


I'm trying to create a DialogFragment contaning a warning dialogue. There is an interface in which there is a method which should be called by pressing the Cancel / OK button of the dialog.

The problem is now that when I have a toast on the user OK / cancel I want to display Mac text, but I do not know in which context this toast should be given, not to crash it. My app crashes when user clicks on the dialog button and I think the context is causing exception:

piece of class:

  public class AlertDFragment DialogFragment extension Does {dialogListener ds; Context associated; Public interface dialog writer {OK on OK (reference C); Public Zero (Reference C) on Cancellation; } @ Override Public Dialog on Critical Dialeg (Bundle Saved Instances) {Builder B = New Alert Dialogue. Builder (getActivity ()); B.setIcon (R.drawable.abc_ic_search); B.setTitle ("DialogFragment Example"); B.setMessage ("Choose an option"); Basset Poitative Button ("OK", New Dialogue Interface. OnClickLaster () {Click on Public Zero (Dialogue Interface Dialog, Into Joe) {ds.onOK (Con);}}); B.setNegativeButton ("Cancel", New DialogInterface.OnClickListener () {Click Public Zero (DialogInterface arg0, int arg1) {ds.onCancel (con);}}); Return b.create (); } Public Alert Defragment () {} Protected Zero Invatch (Activity Activity) {Super. Antache (activity); Con = activity; Try the {Ds = (Dialoglaster) activity; } Catch (class class expansion e) {new class class exposure (activity. To string) + "must implement the dialog dialog interface"); Main activity (parent of the piece):  
  The public class increases the main activity fragmentation activity tools AlertDFragment.dialogListener {button dialog2 ; Fragment Manager FM = Millennial Support Fragmentation Manager (); @ Override Protected Zero (Bundle Saved Instantstate) {Super. NET (Saved Instantstate); SetContentView (R.layout.activity_main); Dialog2 = (button) findViewById (R.id.btn2); dialog2.setOnClickListener (New OnClickListener () {@Override public void onClick (View arg0) {AlertDFragment ADF = new AlertDFragment (); adf.show (FM, "My Tags");}}); } // Interface methods public at oak (reference c) {toast. Make Text (main activity., "OK pressed", toast. LNNGHORART). Show (); Cancel on Public Nil (Reference C) {Toast. Maketext (main activity., "Press cancellation", toast. LNNGHHORT). Show (); } @ Override Public Boolean On Crate Option Menu (Menu Menu) {// Expand Menu; It adds the item to the Action Bar if it exists GetMenuInflater (). Fluo (R. menu menu, menu); Back true; } @ Override Public Boolean On Option Itam Select (Menu Item) {// Handle Action Bar Item Click here Action Bar will automatically click on the Home / Up button, so long as you // in AndroidManifest.xml Specify basic activity. Int id = item.getItemId (); If (id == R.id.action_settings) {return true; } Return Super.Options item selected (item); }  

}

and logcat:

  11-04 10: 23: 30.9 9 5: E / Android Random (2014 ): Fatal Exceptions: Main 11-04 10: 23: 30.9 5 9: E / Android Random (2014): Process: com.apex.dialogtest, PID: 2014 11-04 10: 23: 30.9 5 9: E / Android Rentaim (2014): java.lang .NullPointerException 11-04 October: 23: 30.959: e / AndroidRuntime (2014): on com.apex.dialogtest.AlertDFragment $ 1.onClick (AlertDFragment.java:33) 11-04 October: 23: 30.959: e / AndroidRuntime (2014): 23 :: 30.959: e / AndroidRuntime (2014): on android.os.Handler.dispatchMessage (handler com.android.internal.app.AlertController $ ButtonHandler.handleMessage (AlertController.java : 166) In October 11-04, Java: 102) 11-04 10: 23: 30.9 5 9: E / A Rroid Rentaim (2014): Androidkos. Looper Loop (Looper.java:1236) 11-04 10: 23: 30.9 5 9: E / Android Rentaim (2014): android.app.ActivityThread.main (ActivityThread.java:5017) in Ll-04 Aktubr: 23: 30.959 : E / AndroidRuntime (2014): at java.lang.reflect.Method.invokeNative (Native Method) October 11-04: 23: 30.9 5 9: E / Android Random (20 14): java.lang.reflect.Method 11-04 on .invoke (Method.java:515) October: 23: 30.959: e / AndroidRuntime (2014): on com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:779) October 11- 04: 23: 30.959: e / AndroidRuntime (2014): com.android.internal.os.ZygoteInit.main (ZygoteInit.java:595) in Ll-04 Aktubr: 23: 30.959: e / AndroidRuntime (2014): Dalvik. Thanksgiving at system.NativeStart.main (Basic Method)  

Thanksgiving !

ds can not be started because protected void atte ( Activity activity) requires lowercase o Always make sure to use override annotation and you get an error.


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -