Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[Android]Object cannot be cast to java.lang.String Asynctask

I got stuck at this error.

java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]

This is full code.

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edTenDangNhap =(EditText) findViewById(R.id.edTenDangNhap);
    edMatKhau =(EditText) findViewById(R.id.edMatKhau);
    btnDangKi =(Button) findViewById(R.id.btnDangKi);
    btnDangNhap =(Button) findViewById(R.id.btnDangNhap);

    btnDangNhap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String tentk = edTenDangNhap.getText().toString();
            String matkhau = edMatKhau.getText().toString();

            // ==== I execute AsyncTask there
            AsyncTask dangnhap = new AsyncDangNhap();
            dangnhap.execute(tentk,matkhau); // IDE announce there : JDK 5.0 only.  Unchecked to call execute Params ...
        }
    });
}

public class AsyncDangNhap extends AsyncTask<String[], Void, Integer>{//error there
    @Override
    protected Integer doInBackground(String[]... params) {
        WebService sv = new WebService();
        int kiemtra = sv.KiemTraDangNhap(params[0],params[1]);
        return kiemtra;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Toast.makeText(getApplicationContext(),"Dang xu li ... !",Toast.LENGTH_LONG).show();
    }
    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        if(result >0){
            //Dang nhap thanh cong
            Toast.makeText(getApplicationContext(),"Dang nhap thanh cong !",Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(getApplicationContext(),"Dang nhap that bai !",Toast.LENGTH_LONG).show();
        }
    }
}`
like image 374
Giang Strider Avatar asked Mar 27 '26 14:03

Giang Strider


2 Answers

Change String[] to String in AsyncTask and doInBackground method because currently passing Strings in dangnhap.execute method instead of String Array.like:

public class AsyncDangNhap extends AsyncTask<String, Void, Integer>{ 
    @Override
    protected Integer doInBackground(String... params) {
         .....
    }

  ....
}
like image 113
ρяσѕρєя K Avatar answered Mar 29 '26 02:03

ρяσѕρєя K


As noted in the exception, you are trying to pass strings to the super class async task execute method of your async task object which takes a varag (variable length argument which is basically an array) of objects.

To fix this problem, simply replace the line

AsyncTask dangnhap = new AsyncDangNhap();

With

AsyncDangNhap dangnhap = new AsyncDangNhap();
like image 44
Rex. A Avatar answered Mar 29 '26 02:03

Rex. A