skip to Main Content

I’m trying to connect my project app with an online database through cPanel Hosting. I used the AlertDialog.Builder, setOnClickListener,
for showing the response of the server but it seems i got some error any answers will be appreciated Thanks

here is my java file.

//Calling my AsyncTask
@Override
 public void onClick(View view) {
 view.setEnabled(false);
 Intent launchactivity = new Intent(MainActivity.this,MainActivity.class);
 startActivity(launchactivity);

            tvscancontent = tvScanContent.getText().toString();
            tvscanformat = tvScanFormat.getText().toString();
            BackgroundTask backgroundTask = new BackgroundTask();
            backgroundTask.execute(tvscancontent,tvscanformat);
            finish();

            showShowView2();
           // displaynotification();

        }

    });

private class BackgroundTask extends AsyncTask<String,Void,String> { // i got crash here

        Context context;
        String add_info_url;
        private Object activity;




        public BackgroundTask() {

        }

        @Override
        protected void onPreExecute() {


            add_info_url = "http://www.d48.asia/shan/index.php";

            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setTitle("Processing...");
            progressDialog.setMessage("Authenticating...");
            progressDialog.setCancelable(false);
            progressDialog.setIndeterminate(true);
            progressDialog.setProgressStyle(progressDialog.STYLE_SPINNER);
            progressDialog.show();



        }

        @Override
        protected String doInBackground(String... args) {

            String tvscancontent = args[0];
            String tvscanformat = args[1];
            String response = "";
            String line = "";

            try {
                URL url = new URL(add_info_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String data_string = URLEncoder.encode("tvscancontent", "UTF-8") + "=" + URLEncoder.encode(tvscancontent, "UTF-8")+ "&" +
                        URLEncoder.encode("tvscanformat", "UTF-8") + "=" + URLEncoder.encode(tvscanformat, "UTF-8");
                bufferedWriter.write(data_string);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));

                while ((line = bufferedReader.readLine()) != null){

                    response+= line;


                }


                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();

                return response;


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            return null;


        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);

        }

     //Server response
        @Override
        protected void onPostExecute(final String result) {
            //Toast toast =Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG);
            //toast.setGravity(Gravity.CENTER ,0,0);
            //toast.show();
                AlertDialog.Builder alert = new AlertDialog.Builder(context); // also i got crash here
                    alert.setTitle("Third Wheel Response");
                    alert.setMessage(result);
                    alert.setCancelable(false);
                    alert.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                          dialogInterface.cancel();
                        }
                    });
                    AlertDialog dialogs = alert.create();
                    dialogs.show();


                if (progressDialog != null) {
                    progressDialog.dismiss();
                    SaveData.setEnabled(true);

                }

ERRORS:

11-10 08:40:23.174 4815-4815/com.thethirdwheelproject.thirdwheel E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.thethirdwheelproject.thirdwheel, PID: 4815
android.view.WindowManager$BadTokenException: Unable to add window — token android.os.BinderProxy@bc0afcf is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:678)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:316)
at com.thethirdwheelproject.thirdwheel.MainActivity$BackgroundTask.onPostExecute(MainActivity.java:489)
at com.thethirdwheelproject.thirdwheel.MainActivity$BackgroundTask.onPostExecute(MainActivity.java:389)
at android.os.AsyncTask.finish(AsyncTask.java:660)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
11-10 08:40:23.733 1334-1334/? E/EGL_emulation: tid 1334: eglCreateSyncKHR(1901): error 0x3004 (EGL_BAD_ATTRIBUTE)

3

Answers


  1. Chosen as BEST ANSWER
            //Calling my AsyncTask
    @Override
            public void onClick(View view) {
                view.setEnabled(false);
    
                tvscancontent = tvScanContent.getText().toString();
                tvscanformat = tvScanFormat.getText().toString();
                BackgroundTask backgroundTask =new BackgroundTask();
                backgroundTask.execute(tvscancontent,tvscanformat);
    
                showShowView2();
               // displaynotification();
    
            }
    
        });
    

    //inserting data into database process private class BackgroundTask extends AsyncTask {

        String add_info_url;
    
    
        public BackgroundTask() {
    
        }
    
        @Override
        protected void onPreExecute() {
    
            add_info_url = "http://www.d48.asia/shan/index.php";
    
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setTitle("Authentication Process");
            progressDialog.setMessage("Loading....");
            progressDialog.setCancelable(false);
            progressDialog.setIndeterminate(true);
            progressDialog.setProgressStyle(progressDialog.STYLE_SPINNER);
            progressDialog.show();
    
        }
    
        @Override
        protected String doInBackground(String... args) {
    
            String tvscancontent = args[0];
            String tvscanformat = args[1];
            String response = "";
            String line = "";
    
            try {
                URL url = new URL(add_info_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String data_string = URLEncoder.encode("tvscancontent", "UTF-8") + "=" + URLEncoder.encode(tvscancontent, "UTF-8")+ "&" +
                        URLEncoder.encode("tvscanformat", "UTF-8") + "=" + URLEncoder.encode(tvscanformat, "UTF-8");
                bufferedWriter.write(data_string);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
    
                while ((line = bufferedReader.readLine()) != null){
                    response+= line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
    
                return response;
    
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return null;
    
        }
    
        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
    
        }
    
        @Override
        protected void onPostExecute(final String result) {
            super.onPostExecute(result);
                    alert = new AlertDialog.Builder(MainActivity.this);
                    alert.setTitle(" Third Wheel Response");
                    alert.setMessage(result);
                    alert.setCancelable(false);
                    alert.setPositiveButton("Thanks", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Intent launchactivity = new Intent(MainActivity.this,MainActivity.class);
                            startActivity(launchactivity);
    
    
                        }
                    });
                    AlertDialog dialogs = alert.create();
                    dialogs.show();
    
                if (progressDialog != null) {
                    progressDialog.dismiss();
                    SaveData.setEnabled(true);
    
                }
    
    
        }
    

  2. set the context in your constructor,
    something like below

    public BackgroundTask(Context context) {
        this.context = context;
    }
    
    Login or Signup to reply.
  3. Your context variable is not initialized

    Remove your context variable and use instead MainActivity.this.

    Try this way

    @Override
     public void onClick(View view) {
     view.setEnabled(false);
                tvscancontent = tvScanContent.getText().toString();
                tvscanformat = tvScanFormat.getText().toString();
                BackgroundTask backgroundTask = new BackgroundTask();
                backgroundTask.execute(tvscancontent,tvscanformat);
                showShowView2();
               // displaynotification();
    
            }
    
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search