0

After executing a http request in a DialogFragment, my screen comes to the main activity screen from the screen showing up the DialogFragment. I want the screen not to move after above execution. I hope you have some ideas about this.

This is the DialogFragment class

public class fragmentForRm extends DialogFragment {
    String url3 ="http://192.168.3.3:8080/html/apiDelete.php";

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_for_delete)
                .setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());
                        StringRequest stringRequest = new StringRequest(Request.Method.POST, url3,
                                new Response.Listener<String>(){
                                    @Override
                                    public void onResponse(String response){
                                        Toast.makeText(getActivity().getApplicationContext(), response.trim(), Toast.LENGTH_LONG).show();
                                    }
                                }, new Response.ErrorListener() {

                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(getActivity().getApplicationContext(), error.toString(), Toast.LENGTH_LONG).show();
                            }
                        }){
                            @Override
                            protected Map<String, String> getParams(){
                                Map<String, String> params = new HashMap<String, String>();
                                Bundle bundle = getArguments();
                                String t = bundle.getString("threadForDelete", "");
                                params.put("delete_thread_content", t);
                                return params;
                            }
                        };
                        queue.add(stringRequest);
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                    }
                });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

This is the method call for DialogFragment

 private void rmThreadListViewListener(){
        listView.setOnItemLongClickListener(
                new AdapterView.OnItemLongClickListener(){
                    @Override
                    public boolean onItemLongClick(AdapterView<?> adapter,
                                                   View view, int pos, long id){
                        String selectedThread = (String) adapter.getItemAtPosition(pos);
                        fragmentForRm dialogFragment = new fragmentForRm();
                        Bundle bundle = new Bundle();
                        bundle.putString("threadForDelete", selectedThread);
                        dialogFragment.setArguments(bundle);
                        FragmentManager fm = getSupportFragmentManager();
                        dialogFragment.show(fm, null);
                        return true;
                    }
                }
        );

    }

This is an error code after executing the http request from the DialogFragment

W/Settings: Setting device_provisioned has moved from android.provider.Settings.Secure to android.provider.Settings.Global.
V/HiTouch_HiTouchSensor: User setup is finished.
V/AudioManager: querySoundEffectsEnabled...
W/libEGL: EGLNativeWindowType 0x7167100390 disconnect failed
D/OpenGLRenderer: endAllActiveAnimators on 0x70fa508600 (RippleDrawable) with handle 0x715a9da300
I/BlockMonitor: dispatchingThrewException In MainThread
D/AndroidRuntime: Shutting down VM
I/QarthLog: [PatchStore] createDisableExceptionQarthFile
    [PatchStore] create disable file for com.example.testjsondisplay uid is 10270
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.testjsondisplay, PID: 11947
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context androidx.fragment.app.FragmentActivity.getApplicationContext()' on a null object reference
        at com.example.testjsondisplay.fragmentForRm$2$1.onResponse(fragmentForRm.java:38)
        at com.example.testjsondisplay.fragmentForRm$2$1.onResponse(fragmentForRm.java:35)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8147)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
I/Process: Sending signal. PID: 11947 SIG: 9
Connected to process 12222 on device 'huawei-mar_lx2j-QUR0219B14005574'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/testjsondispla: QarthPatchMonintor::Init
    QarthPatchMonintor::StartWatch
    QarthPatchMonintor::WatchPackage: /data/hotpatch/fwkhotpatch/
    QarthPatchMonintor::CheckAndWatchPatch: /data/hotpatch/fwkhotpatch/com.example.testjsondisplay
    QarthPatchMonintor::CheckAndWatchPatch: /data/hotpatch/fwkhotpatch/all
    QarthPatchMonintor::Run
I/testjsondispla: QarthPatchMonintor::Reading
    QarthPatchMonintor::CheckNotifyEvent
    QarthPatchMonintor::CheckNotifyEvent before read
I/HwApiCacheMangerEx: apicache path=/storage/emulated/0 state=mounted key=com.example.testjsondisplay#10270#256
I/HwApiCacheMangerEx: apicache path=/storage/emulated/0 state=mounted key=com.example.testjsondisplay#10270#0
I/AwareBitmapCacher: init processName:com.example.testjsondisplay pid=12222 uid=10270
E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@fff406e
V/ActivityThread: callActivityOnCreate
V/HwWidgetFactory: : successes to get AllImpl object and return....
I/OverScrollerOptimization: start init SmartSlideOverScroller and get the overscroller config
    get the overscroller config
W/testjsondispla: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/testjsondispla: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
D/ActivityThread: add activity client record, r= ActivityRecord{a741a53 token=android.os.BinderProxy@1ab7ded {com.example.testjsondisplay/com.example.testjsondisplay.MainActivity}} token= android.os.BinderProxy@1ab7ded
D/HiTouch_PressGestureDetector: onAttached, package=com.example.testjsondisplay, windowType=1, mHiTouchRestricted=false
I/iGraphics: [0020080c] pn: com.example.testjsondisplay, p: 12222
    [0030080c] no spt app: com.example.testjsondisplay
D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
W/Gralloc3: mapper 3.x is not supported
E/: APS:IFLoad:importExternalFunctions, search function createNewHwApsUtils failed, dlsym err:undefined symbol: createNewHwApsUtils
D/: APS:importExternalFunctions OK
I/HwViewRootImpl: removeInvalidNode jank list is null
Katelyn
  • 41
  • 1
  • 5
  • @Selvin I couldn't find any answer with my problem. But, I will get some knowledge about DialogFragment. Thanks! – Katelyn Feb 24 '22 at 11:41
  • The probelm is obvious ... after positive button click dialog is closed and fragment detached ... so calling `getActivity()` will obviously return null ... you should **prevent dialog from closing** and close when async request finished – Selvin Feb 24 '22 at 11:46
  • @RyanM I found out that the problem is `Toast.makeText(getActivity().getApplicationContext(), response.trim(), Toast.LENGTH_LONG).show();` and `getActivity().getApplicationContext()` returns null somehow. I knew a stack trace is important for solving errors. Thank you! – Katelyn Feb 24 '22 at 11:58
  • @Selvin I found the answer is on your post, too and know why the problem happens. Thanks! – Katelyn Feb 25 '22 at 16:05

0 Answers0