android – AsyncTask和IllegalArgumentException

前端之家收集整理的这篇文章主要介绍了android – AsyncTask和IllegalArgumentException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我正在开发的应用程序中,当我触摸设备的屏幕时,我连接到服务器并使用AsyncTask显示忙碌的指示器,并且在此步骤中我没有
问题.但是当我连接并按下home buttom“应用程序进入onPause”并且我将应用程序再次显示为“onResume”并触摸屏幕
再次连接到服务器,我收到以下发布的logCat错误.

注意:在onPause我urnRegister WiFi接收器,我断开与服务器的连接. logCat输出显示AsyncTask的调用回调序列

我不知道为什么我收到IllegalArgumentException,我读了一些关于它的postes,我测试了名为“client”的对象,它永远不会为null

logcat的:

03-09 14:26:13.413: D/MainActivity(17065): @MQTTAsynchTask(): constructor called
03-09 14:26:13.413: D/MainActivity(17065): @MQTTAsynchTask(): client is not null
03-09 14:26:13.413: D/MainActivity(17065): @MQTTAsynchTask(): onPreExecute().
03-09 14:26:13.422: D/MainActivity(17065): @MQTTAsynchTask(): doInBackground().
03-09 14:26:13.433: E/AndroidRuntime(17065): FATAL EXCEPTION: pool-1-thread-1
03-09 14:26:13.433: E/AndroidRuntime(17065): Process: com.example.mqtt_designlayout_02,PID: 17065
03-09 14:26:13.433: E/AndroidRuntime(17065): java.lang.IllegalArgumentException: Invalid ClientHandle
03-09 14:26:13.433: E/AndroidRuntime(17065):    at org.eclipse.paho.android.service.MqttService.getConnection(MqttService.java:552)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at org.eclipse.paho.android.service.MqttService.connect(MqttService.java:318)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at org.eclipse.paho.android.service.MqttAndroidClient.doConnect(MqttAndroidClient.java:427)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at org.eclipse.paho.android.service.MqttAndroidClient.access$2(MqttAndroidClient.java:417)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at org.eclipse.paho.android.service.MqttAndroidClient$1.run(MqttAndroidClient.java:395)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-09 14:26:13.433: E/AndroidRuntime(17065):    at java.lang.Thread.run(Thread.java:818)

AsynchTask

public MQTTAsynchTask(Context contex,MqttAndroidClient client,MqttConnectOptions opts) {
        // TODO Auto-generated constructor stub
        Log.d(TAG,"@MQTTAsynchTask(): constructor called");
        this.context = contex;
        this.MQTTAndroidClient = client;
        if (client != null) {
            Log.d(TAG,"@MQTTAsynchTask(): client is not null");
        }
        this.opts = opts;
    }
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        Log.d(TAG,"@MQTTAsynchTask(): onPreExecute().");

        dialog = new Dialog(this.context);
        dialog.setCancelable(false);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.progressdialog);
        progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar1);
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        Log.d(TAG,"@MQTTAsynchTask(): doInBackground().");
        do {
            try {
                this.MQTTAndroidClient.connect(this.opts,this.context,synchCONNCallBack);
            } catch (MqttSecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MqttException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                Thread.sleep(MQTT_BROKER_TIME_OUT);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            this.totalTimeOut += MQTT_BROKER_TIME_OUT;

        } while ( (!this.isCancelled()) && (this.MQTTAndroidClient != null) && (!this.MQTTAndroidClient.isConnected()) && (this.totalTimeOut <= (10 * MQTT_BROKER_TIME_OUT)) );

        return null;
    }

在onPause:

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Log.w(TAG,"@onPause()");

    this.onPauseCalled = true;

    if (reconnectTimerCurrentlyActive) {
        reconnectTimerCurrentlyActive = false;
        reconnectTimer.cancel();
        reconnectTimer.purge();
        Log.v(TAG,"reconnect timer set to 'false',and reconnectTimer is cancelled");
    }

    if (this.MQTTAsynch != null) {
        Log.d(TAG,"asynchTask object was initilised");
        if (this.MQTTAsynch.getStatus() == AsyncTask.Status.PENDING) {
            Log.d(TAG,"AsynchTask has not started yet.");
            boolean cancelledSuccessully = this.MQTTAsynch.cancel(true);
            if (cancelledSuccessully) {
                Log.d(TAG,"AsynchTask is cancelled successfully.");
            } else {
                Log.d(TAG,"AsynchTask Failed to cancell");
            }
        }
        if (this.MQTTAsynch.getStatus() == AsyncTask.Status.RUNNING) {
            Log.d(TAG,"AsynchTask still running doing work in the backgroung thread,and it will be intrrupted");
            boolean cancelledSuccessully = this.MQTTAsynch.cancel(true);
            if (cancelledSuccessully) {
                Log.d(TAG,"AsynchTask Failed to cancell");
            }
        }
        if (this.MQTTAsynch.getStatus() == AsyncTask.Status.FINISHED) {
            Log.d(TAG,"AsynchTask has finished its work.");
        }
    } else {
        Log.d(TAG,"asynchTask object was not initilised. this.MQTTAsynch == null");
    }

    if ( (this.subActivityReturned) || (this.isConnectCalled) ) {
        MQTT_Disconnect_Module();
    }

    if (MQTTPrimaryReceiverRegistered) {
        unregisterReceiver(MQTTPrimaryReceiver);
        Log.v(TAG,"BroadCastReceiver (MQTTPrimaryReceiver) unregistered");
        MQTTPrimaryReceiverRegistered = false;
    }
}

解决方法

我认为首先要学习的是完全阅读ErrorLogs并理解它. stacktrace的第一行说明了这一点
03-09 14:26:13.433: E/AndroidRuntime(17065): java.lang.IllegalArgumentException: Invalid ClientHandle
03-09 14:26:13.433: E/AndroidRuntime(17065):    at org.eclipse.paho.android.service.MqttService.getConnection(MqttService.java:552)

错误方法getConnection()中的MqttService类中开始.方法getConnection()抛出IllegalArgumentException而不是AsyncTask.它恰好是从AsyncTask内部调用的.此外,变量ClientHandle不是Android API的一部分,它是MqttService的一部分.该变量实际上是无效的并且导致异常.

如果您无法找出变量ClientHandle为无效的原因,您也可以共享您的MqttService类.也许这里的人可以帮助你,但是根据所提供的信息,这是我能回答的问题.希望这可以帮助.

猜你在找的Android相关文章