0

首先,我想为我糟糕的英语道歉。

我正在使用从 Main_Activity 调用的 AsyncTask 类,当在 PostExecute 中调用 de Intent 时,我收到错误“Not Enclosure Instance type is accesible in scope”

package com.example.pfc;

import android.os.Bundle;
import android.app.Activity;`enter code here`
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Principal extends Activity {
    private Button BotonSalir;
    private Button BotonAtras;
    private Button BotonClientes;
    private Button BotonMaquinas;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);

        BotonSalir = (Button) findViewById(R.id.BotonSalir);
        BotonAtras = (Button) findViewById(R.id.BotonAtras);
        BotonClientes = (Button) findViewById(R.id.BotonClientes);
        BotonMaquinas = (Button) findViewById(R.id.BotonMaquinas);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_principal, menu);
        return true;
    }

    public void Atras(View view){
        finish();
    }

    public void ListaClientes(View view){

        SendClientes send = new SendClientes();
        send.execute("clientes");
    }
    public void ListaMaquinas(View view){

        SendMaquinas send = new SendMaquinas();
            send.execute("servidores");
        }

    }

现在异步任务“发送”:

package com.example.pfc;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeoutException;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Address;
import com.rabbitmq.client.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.RpcClient;
import com.rabbitmq.client.ShutdownSignalException;


public class SendClientes extends AsyncTask <String,Void,Void> {
private String requestQueueName = "rpc_queue";
private String replyQueueName;
private QueueingConsumer consumer;

    protected Void doInBackground(String... p) {

        String mensaje = p[0];
        ConnectionFactory cFactory = new ConnectionFactory();
        Connection connection;

        //JSON Object

        JSONObject json = new JSONObject();
        try {
        json.put("Request", mensaje);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //Conexion
        try {
            Address[] addrs= new Address[1];
            addrs[0] = Address.parseAddress("84.126.242.244");

            connection = cFactory.newConnection(addrs);
        cFactory.setPort(5672);
        Channel channel = connection.createChannel();
        replyQueueName = channel.queueDeclare().getQueue();
        consumer = new QueueingConsumer(channel);
        channel.basicConsume(replyQueueName, true,consumer);

        String corrId = java.util.UUID.randomUUID().toString();

        //Propiedades enviadas al servidor
        AMQP.BasicProperties props = new AMQP.BasicProperties()
                            .builder()
                            .correlationId(corrId)
                            .replyTo(replyQueueName)
                            .build();
        channel.basicPublish("", replyQueueName, props,     json.toString().getBytes());

        //Esperando respuesta del servidor
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            if (delivery.getProperties().getCorrelationId().equals(corrId))
            {
                String respuesta = new String(delivery.getBody());
                break;
            }
        }


        //Eliminamos la cola del servidor, tras haber recibido el mensaje
        channel.queueDelete(replyQueueName);
        //cerramos canal y conexion
        channel.close();
        connection.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ShutdownSignalException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ConsumerCancelledException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    // TODO Auto-generated method stub
    return null;


}

//Capturamos la respuesta en el posexecute
protected void onPostExecute(String respuesta) {
    //Lanzamos el intent a la actividad de usuarios, poniendo en el intent la respuesta del servidor.


    Intent intent = new Intent(Principal.this, Activity_Usuarios.class); //Error "No enclosing instance of the type Principal is accessible in scope"


}

}

提前致谢。

再见!

4

1 回答 1

0

我认为您在意图中提供了错误的上下文。我建议您在 Principal Activity 中将 SendClientes asynctask 设为私有。或者创建另一个将 SendClientes 作为内部私有类的类,并通过构造函数提供上下文(如果您希望将 Principal 和 SendClientes 类分开)。

类似问题: 在范围内无法访问 MainActivity 类型的封闭实例

于 2013-05-13T09:00:52.310 回答