我开始使用andriod进行编程,而我在使用AsyncTask时遇到了一些麻烦......
我一直在阅读它,但现在我不知道如何处理这个错误:
Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.Void[]
at reminder.app.Downloader$Receiver.doInBackground(Downloader.java:88)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
它似乎试图在执行该任务的内部过程中向Void []投射内容。
这是我的AsyncTask代码:
private class Receiver extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
connect();
if (_connected) receive();
return null;
}
@Override
public void onPostExecute(Void result) {...}
}
我认为recevie()和connect()方法与错误无关。
无论如何这里是代码:
private void connect() {
try {
_socket = new Socket(_url, _port);
_in = new Scanner(_socket.getInputStream());
_out = new PrintWriter(_socket.getOutputStream());
_connected = true;
} catch (Exception e) {
_connected = false;
}
}
public String recieve() {
_response = "";
try {
_out.println("GET");
_out.flush();
while (!_in.hasNext()) ;
_response = _in.nextLine();
} catch (Exception e) {
_connected = false;
}
}
这里我调用执行MainActivity类:
public class MainActivity extends ActionBarActivity {
...
AsyncTask<Void,Void,Void> task;
protected void onCreate(Bundle savedInstanceState) {
...
downloader= new Downloader(_ADDRESS,_PORT,this,"");
task = downloader.getReceiver();
task.execute();
...
}
public AsyncTask<Void,Void,Void> getReceiver(){
return new Receiver();
}
为了改进,我会告诉你任何事情!谢谢!
解决!
我忘了正确声明任务变量:
AsyncTask task;
而不是:
AsyncTask<Void,Void,Void> task;
现在我感到愚蠢的XD