0

First of all, I want to apologize for my BAD English.

I'm using a AsyncTask class, called from Main_Activity, and when in PostExecute I call de intent, I get an error "Not Enclosing 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");
        }

    }

And now the asynctask "Send":

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"


}

}

Thanks in advance.

Bye!

1 Answers1

0

I think you are providing the wrong context in the intent. I suggest you make the SendClientes asynctask private within the Principal Activity. Or create another class that has SendClientes as an inner private class, and provide the context through the constructor (if you want the Principal and SendClientes classes to be seperated).

Similar problem: No enclosing instance of the type MainActivity is accessible in scope

Community
  • 1
  • 1
Vincent Ketelaars
  • 1,069
  • 1
  • 14
  • 35