Error in AlertDialog

0

I'm having a problem submitting a alert dialog to my app , the following error appears:

  

Unable to add window is not valid for your running activity?

Can anyone help me solve it?

Code:

package imm.pt.immsmart;

                import android.content.Intent;
                import android.support.v7.app.ActionBar;
                import android.support.v7.app.AlertDialog;
                import android.support.v7.app.AppCompatActivity;
                import android.os.Bundle;
                import android.view.View;
                import android.widget.TextView;
                import android.widget.Toast;

                import org.json.JSONException;
                import org.json.JSONObject;

                public class confChange extends AppCompatActivity {

                    @Override
                    protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_conf_change);
                        ActionBar actionBar = getSupportActionBar();
                        actionBar.hide();
                        Intent intent = getIntent();
                        Bundle bundle = intent.getExtras();
                        int tar = bundle.getInt("tari");
                        TextView txtconf = (TextView) findViewById(R.id.txtConf);
                        if(tar == 1){
                            txtconf.setText("Pretendes realmente alterar o teu tarifário para o UFC 5GB?");
                        }else{
                            txtconf.setText("Pretendes realmente alterar o teu tarifário para o UFC 1GB?");
                        }

                    }
                    public void nao(View view){
                        Intent intent = new Intent(this, changetar.class);
                        startActivity(intent);
                    }
                    public void sim(View view){
                        TextView txtconf = (TextView) findViewById(R.id.txtConf);
                        String txt = (String) txtconf.getText();
                        if(txt == "Pretendes realmente alterar o teu tarifário para o UFC 5GB?"){
                            callSrv("changecincogiga", "");
                        }else{
                            callServer("changeumgiga", "");
                        }

                    }
                    private void callServer(final String method, final String data){
                        new Thread(){
                            public void run() {
                                String answer = HttpConnection.getSetDataWeb("http://192.168.1.70/myIMM/api.php", method, data);
                                degenerateJSON(answer);
                            }
                        }.start();
                    }
                    private void callSrv(final String method, final String data){
                        new Thread(){
                            public void run() {
                                String answer = HttpConnection.getSetDataWeb("http://192.168.1.70/myIMM/api.php", method, data);
                                cinco(answer);
                            }
                        }.start();
                    }
                    private void degenerateJSON(final String data){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try{
                                    JSONObject jo = new JSONObject(data);
                                    int perm = jo.getInt("perm");
                                    if(perm == 0){
                                        AlertDialog.Builder builder;
                                        builder = new AlertDialog.Builder(getApplicationContext(), R.style.Theme_AppCompat_Dialog_Alert);
                                        builder.setTitle("Erro").setMessage("O teu tarifário ja é UFC 1GB").show();
                                    }else{
                                        AlertDialog.Builder builder;
                                        builder = new AlertDialog.Builder(getApplicationContext(), R.style.Theme_AppCompat_Dialog_Alert);
                                        builder.setTitle("Sucesso").setMessage("Tarifário alterado para UFC 1GB").show();
                                    }

                                }catch(JSONException e) {e.printStackTrace();}
                            }
                        });

                    }
                    private void cinco(final String data){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try{
                                    JSONObject jo = new JSONObject(data);
                                    int perm = jo.getInt("perm");
                                    if(perm == 0){
                                        AlertDialog.Builder builder;
                                        builder = new AlertDialog.Builder(getApplicationContext(), R.style.Theme_AppCompat_Dialog_Alert);
                                        builder.setTitle("Erro").setMessage("O teu tarifário ja é UFC 5GB").show();
                                    }else{
                                        AlertDialog.Builder builder;
                                        builder = new AlertDialog.Builder(getApplicationContext(), R.style.Theme_AppCompat_Dialog_Alert);
                                        builder.setTitle("Sucesso").setMessage("Tarifário alterado para UFC 5GB").show();
                                    }

                                }catch(JSONException e) {e.printStackTrace();}
                            }
                        });

                    }
                    public void volta(View view){
                        Intent intent = new Intent(this, changetar.class);
                        startActivity(intent);
                    }
                }
    
asked by anonymous 04.09.2018 / 18:43

1 answer

0

You're overloading main thread, you should create an internal thread-extending class.

CallServer thread = new CallServer();
thread.start();


class CallServer extends thread{...

@OVerride
public void run(){
      String answer = HttpConnection.getSetDataWeb("http://192.168.1.70/myIMM/api.php", method, data);
                                degenerateJSON(answer);
}
   
}
    
10.09.2018 / 00:39