Error in Alertdialog

Asked

Viewed 109 times

0

I’m having a problem presenting a alert dialog in my app, appears the following error:

Unable to add window is not Valid is your Activity running?

Can someone help me solve?

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);
                    }
                }
  • which line gives the error?

  • In a line like this: Builder.setTitle("Error"). setMessage("Your tariff is already UFC 1GB"). show();

1 answer

0

You are overloading main thread, you must create an internal class extending thread.

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);
}
   
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.