warning: window already focused ignoring focus gai

2019-09-19 14:58发布

问题:

i have search and not found the answer. I want to call startActvity(newintent), but the page not change. This my code:

public class MainActivity extends Activity {
private TextView IdPeserta, Password;
//private ArrayList<NameValuePair> authentication;
@SuppressWarnings("unused")
//private String temp, _nama = "";
//private readURL rL;

public static String username = "";
public static String password = "";
public static String status = "FALSE";

public void setPassword(String password){
    MainActivity.password = password;
}

public void setUsername(String username){
    MainActivity.username = username;
}

public void setStatus(String status){
    MainActivity.status = status;
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);
    IdPeserta = (TextView) findViewById(R.id.idPeserta);
    Password = (TextView) findViewById(R.id.password);
}    

public void Login(View view){
    setUsername(IdPeserta.getText().toString());
    setPassword(Password.getText().toString());
    if((IdPeserta.getText().length() > 0 && Password.getText().length() > 0)){
        RestTask task = new RestTask();
        task.applicationContext = MainActivity.this;
        task.execute();
    }else{
        Toast.makeText(this,"fill Id Peserta and Password 1", 0).show();
    }
}

public void authenticate(){
    if(status.equals("TRUE")){
        Intent i = new Intent(MainActivity.this, Home.class);
        //PendingIntent pending = PendingIntent.getActivity(this, 0, i, 0);
        startActivity(i);
        finish();
    }else if(status.equals("FALSE")){
        Log.d("gagal", "coba lagi");
    }
}

public static String getService() {

    String responseString = null;

    String baseurlString = "http://10.0.2.2:8080/UjianServices/authentikasi.php";

    RestClient client = new RestClient(baseurlString);
    client.AddParam("id_peserta", MainActivity.username);
    client.AddParam("password", MainActivity.password);

    try {
        client.Execute(RequestMethod.POST);
    } catch (Exception e) {
        Log.d("error", e.getMessage());
        //e.printStackTrace();
    }

    responseString = client.getResponse();

    return responseString;
}

public class RestTask extends  AsyncTask<Object, Object, Object>{
    private ProgressDialog dialog;
    protected Context applicationContext;

    @Override
    protected void onPreExecute() {
        this.dialog = ProgressDialog.show(applicationContext, "Calling", "Time Service...", true);
    }

    @Override
    protected void onPostExecute(Object result) {
        this.dialog.cancel();
        String status = result.toString();
        setStatus(status);
        authenticate();
    }

    @Override
    protected Object doInBackground(Object... arg0) {
        return MainActivity.getService();
    }
}

}

When i did not using the RestTask it can change the page. How i change my activity?

回答1:

because applicationContext is null you have not initialize applicationContext before using it for showing ProgressDialog from onPreExecute .

you need to initialize applicationContext before using for showing ProgressDialog or just use MainActivity.this as :

   @Override
    protected void onPreExecute() {
        applicationContext=MainActivity.this; //<<< initialize context here
        this.dialog = ProgressDialog.show(applicationContext, 
                                 "Calling", "Time Service...", true);
    }