在其他时间调用AsyncTask obj时出现问题-Android
在my Activity类中,实现了UINotifier,用于在调用TimerTask对象时通知Activity类。在我的doInBackground()中,我只调用位于ASyncTask类(connectTask)中的函数。onPostExecute被执行,最后,我调用位于Activity类中的方法“ReConnect()”。 代码及其流程为:在其他时间调用AsyncTask obj时出现问题-Android,android,android-asynctask,Android,Android Asynctask,在my Activity类中,实现了UINotifier,用于在调用TimerTask对象时通知Activity类。在我的doInBackground()中,我只调用位于ASyncTask类(connectTask)中的函数。onPostExecute被执行,最后,我调用位于Activity类中的方法“ReConnect()”。 代码及其流程为: // OF Interface UINotifier @Override public void notifyUI() { Log.i(TAG
// OF Interface UINotifier
@Override
public void notifyUI() {
Log.i(TAG, "GOT MESSAGE FROM MonitorConnection");
monitorTimer.cancel();
Log.i(TAG, "Preparing to Start");
PrepareToStartToConnect();
//publishProgress(4);
int status = 1;
if (status == 1 || status == 2)
ReConnect();
}
// Called on "Connect" button
private void ConnectClicked() {
Log.i(TAG, "Inside Connectclicked");
if (isConnectEligeble()) {
Log.i(TAG, "isConnectionEligible = true");
currentState= CONNECTING;
connectTask = new ConnectTask();
Log.i(TAG, "CReated ins of ConnectTask");
connectTask.execute("");
}
}
private void ReConnect() {
connectTask = null;
if (HttpUtilities.checkInternetConnection()) {
Log.i(TAG, "ABOUT TO RE-CONNECT");
ConnectClicked();
} else {
Log.i(TAG, "No Interne Mesage");
mMessage.setText(R.string.NO_INERNET);
}
return;
}
private void PrepareToStartToConnect() {
monitorTimer = null;
monitorConn = null;
// Make all vars, objects to null
}
// Start TimerTask & Timer object
private void StartTimer() {
Log.i(TAG, "Into Start Timer");
monitorConn = new MonitorConnection(this); // new MonitorConnection(connectTask);
monitorTimer = new Timer();
monitorTimer.schedule(monitorConn, 0, 30000);
}
/**
* ConnectTask AsyncTask class that handles all activities to get connected to the server
*/
private class ConnectTask extends AsyncTask<String, Integer, Boolean> implements UINotifier {
private ProgressDialog dialog = null;
private UServer us = null;
private VPNServer vs = null;
private ConfigData cd = null;
String serverHost = "", errorMsg = "";
private int status = -1; // 0 Connected 1 = ReConnect 2 = Failed to Connect
public ConnectTask() {
dialog = new ProgressDialog(StartUltimate.this);
us = servers.get(selectedRowIndex);
vs = us.getVpnServer(selectedProtocol, selectedPort);
serverHost = us.getServerHost();
}
private void disposeAll() {
// Dispose all objs
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
Log.i(TAG, "Into onPostExecute()");
if (result == false)
mMessage.setText(errorMsg);
else
mMessage.setText("");
dialog.dismiss();
// Clean up all variables of the object
disposeAll();
Log.i(TAG, "FINISHED onPostExecute()");
// Go out of AsyncTask and have control to other method of Activity class
StartTimer();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
selectedServerHost = serverHost.substring(0, serverHost.indexOf('.'));
dialog.setCancelable(false);
dialog.setIcon(R.drawable.icon);
dialog.setTitle("Progessing...");
dialog.setMessage(String.valueOf(R.string.ui_activity_authenticating));
dialog.show();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// Set all updates to UI
return;
}
@Override
protected Boolean doInBackground(String... params) {
Log.i(TAG, "Into doBackground...");
......
Log.i(TAG, "Starting Connect.....");
StartConnect();
return true;
}
@Override
public void notifyUI() {
Log.i(TAG, "GOT MESSAGE FROM MonitorConnection");
monitorTimer.cancel();
Log.i(TAG, "Preparing to Start");
PrepareToStartToConnect();
publishProgress(4);
status = 1;
if (status == 1 || status == 2)
ReConnect();
}
private void StartConnect() {
// This is empty right now
}
} // END OF ConnectTask class
接口UINotifier的
@凌驾
public void notifyUI(){
Log.i(标记“从监视器连接获取消息”);
monitorTimer.cancel();
Log.i(标签“准备启动”);
准备开始连接();
//出版进度(4);
int状态=1;
如果(状态==1 | |状态==2)
重新连接();
}
//在“连接”按钮上调用
私有void ConnectClicked(){
Log.i(标记“内部连接”);
如果(isconnectEligable()){
Log.i(标记“isconnectionqualified=true”);
当前状态=正在连接;
connectTask=新的connectTask();
Log.i(标记“ConnectTask的已创建插件”);
connectTask.execute(“”);
}
}
私有无效重新连接(){
connectTask=null;
if(HttpUtilities.checkInternetConnection()){
Log.i(标记“即将重新连接”);
ConnectClicked();
}否则{
Log.i(标签“无内部消息”);
msessage.setText(R.string.NO_INERNET);
}
返回;
}
私有void PrepareToStartToConnect(){
monitorTimer=null;
monitorConn=null;
//将所有变量、对象设置为空
}
//启动计时器任务和计时器对象
私有void StartTimer(){
Log.i(标记“进入启动计时器”);
monitorConn=newmonitorconnection(this);//newmonitorconnection(connectTask);
monitorTimer=新计时器();
监视器时间表(监视器控制,0,30000);
}
/**
*ConnectTask AsyncTask类,用于处理连接到服务器的所有活动
*/
私有类ConnectTask扩展AsyncTask实现UINotifier{
private ProgressDialog=null;
private UServer us=null;
私有vpn服务器vs=null;
私有ConfigData cd=null;
字符串serverHost=“”,errorMsg=“”;
private int status=-1;//0已连接1=重新连接2=连接失败
公共连接任务(){
dialog=新建ProgressDialog(StartUltimate.this);
us=servers.get(selectedRowIndex);
vs=us.getvpn服务器(selectedProtocol,selectedPort);
serverHost=us.getServerHost();
}
私有无效处置{
//处理所有OBJ
}
@凌驾
受保护的void onPostExecute(布尔结果){
super.onPostExecute(结果);
Log.i(标记“Into onPostExecute()”);
如果(结果==false)
msessage.setText(errorMsg);
其他的
msessage.setText(“”);
dialog.dismise();
//清理对象的所有变量
disposeAll();
Log.i(标记“FINISHED onPostExecute()”);
//退出AsyncTask并控制活动类的其他方法
StartTimer();
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
selectedServerHost=serverHost.substring(0,serverHost.indexOf('.');
对话框。可设置可取消(false);
dialog.setIcon(R.drawable.icon);
对话框.setTitle(“进程…”);
setMessage(String.valueOf(R.String.ui_activity_authentication));
dialog.show();
}
@凌驾
受保护的void onProgressUpdate(整型…值){
super.onProgressUpdate(值);
//将所有更新设置为UI
返回;
}
@凌驾
受保护的布尔doInBackground(字符串…参数){
Log.i(标记“进入doBackground…”);
......
Log.i(标记“启动连接…”);
StartConnect();
返回true;
}
@凌驾
public void notifyUI(){
Log.i(标记“从监视器连接获取消息”);
monitorTimer.cancel();
Log.i(标签“准备启动”);
准备开始连接();
出版进度(4);
状态=1;
如果(状态==1 | |状态==2)
重新连接();
}
专用void StartConnect(){
//这个现在是空的
}
}//ConnectTask类结束
调用StartTimer,MonitorThread通知Activity类并调用ReConnect,inturn调用ConnectClicked。在这方面,,
connectTask=新的connectTask();是我获取此异常和日志的位置:
04-15 17:09:07.501: INFO/Ultimate VPN:(364): Starting Connect.....
04-15 17:09:07.501: INFO/Ultimate VPN:(364): Into onPostExecute()
04-15 17:09:07.501: INFO/Ultimate VPN:(364): FINISHED onPostExecute()
04-15 17:09:07.501: INFO/Ultimate VPN:(364): Into Start Timer
04-15 17:09:07.542: INFO/MON CONN(364): Into StartMonitor
04-15 17:09:07.542: INFO/MON CONN(364): Into checkConnection
04-15 17:09:07.671: WARN/InputManagerService(69): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4073b170
04-15 17:09:08.252: INFO/System.out(364): MonitorConnection : Notifing the GUI
04-15 17:09:08.262: INFO/Ultimate VPN:(364): GOT MESSAGE FROM MonitorConnection
04-15 17:09:08.262: INFO/Ultimate VPN:(364): Preparing to Start
04-15 17:09:18.322: INFO/Ultimate VPN:(364): ABOUT TO RE-CONNECT
04-15 17:09:18.322: INFO/Ultimate VPN:(364): Inside Connectclicked
04-15 17:09:18.322: INFO/Ultimate VPN:(364): isConnectionEligible = true
04-15 17:09:18.331: WARN/dalvikvm(364): threadid=11: thread exiting with uncaught exception (group=0x40015560)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): FATAL EXCEPTION: Timer-0
04-15 17:09:18.351: ERROR/AndroidRuntime(364): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at android.os.Handler.<init>(Handler.java:121)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at android.app.Dialog.<init>(Dialog.java:101)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at android.app.AlertDialog.<init>(AlertDialog.java:63)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at android.app.ProgressDialog.<init>(ProgressDialog.java:80)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at android.app.ProgressDialog.<init>(ProgressDialog.java:76)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.StartUltimate$ConnectTask.<init>(StartUltimate.java:386)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.StartUltimate.ConnectClicked(StartUltimate.java:324)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.StartUltimate.ReConnect(StartUltimate.java:340)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.StartUltimate.notifyUI(StartUltimate.java:316)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.MonitorConnection.setConnected(MonitorConnection.java:43)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.MonitorConnection.checkConnection(MonitorConnection.java:69)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.MonitorConnection.StartMonitor(MonitorConnection.java:61)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at orange.android.vpn.MonitorConnection.run(MonitorConnection.java:52)
04-15 17:09:18.351: ERROR/AndroidRuntime(364): at java.util.Timer$TimerImpl.run(Timer.java:284)
04-15 17:09:18.382: WARN/ActivityManager(69): Force finishing activity orange.android.vpn/.StartUltimate
04-15 17:09:07.501:INFO/Ultimate VPN:(364):正在启动连接。。。。。
04-15 17:09:07.501:INFO/Ultimate VPN:(364):进入onPostExecute()
04-15 17:09:07.501:INFO/Ultimate VPN:(364):已完成onPostExecute()
04-15 17:09:07.501:INFO/Ultimate VPN:(364):进入启动计时器
04-15 17:09:07.542:INFO/MON CONN(364):进入StartMonitor
04-15 17:09:07.542:INFO/MON CONN(364):进入检查连接
04-15 17:09:07.671:WARN/InputManagerService(69):窗口已聚焦,忽略:com.android.internal.view.IInputMethodClient$Stub的聚焦增益$Proxy@4073b170
04-15 17:09:08.252:信息/系统输出(364):监视器连接:通知GUI
04-15 17:09:08.262:INFO/Ultimate VPN:(364):从MonitorConnection获取消息
04-15 17:09:08.262:INFO/Ultimate VPN:(364):准备启动
04-15 17:09:18.322:INFO/Ultimate VPN:(364):即将重新连接
04-15 17:09:18.322:INFO/Ultimate VPN:(364):内部连接
04-15 17:09:18.322:INFO/Ultimate VPN:(364):IsConnectionQualified=true
04-15 17:09:18.331:WARN/dalvikvm(364):threadid=11:线程退出时出现未捕获异常(组=0x40015560)
04-15 17:09:18.351:错误/AndroidRuntime(364):致命异常:计时器-0
04-15 17:09:18.351:错误/AndroidRuntime(364):java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序
04-15 17:09:18.351:ERROR/AndroidRuntime(364):位于android.os.Handler.(Handler.java:121)
04-15 17:09:18.351:ERROR/AndroidRuntime(364):在android.app.Dialog.(Dialog.java:101)
04-15 17:09:18.351:错误/