2013-05-13 20 views
0

首先,我要爲我的英語不好道歉。意向裏面的AsyncTask(範圍不可訪問)所有的

我使用的AsyncTask類,從Main_Activity調用,當PostExecute我叫去的意圖,我得到一個錯誤「不是外圍實例類型的範圍是入店」

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"); 
     } 

    } 

而且現在的AsyncTask提前

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" 


} 

}

感謝: 「發送」。

再見!

回答

0

我認爲你提供的意圖錯誤的上下文。我建議你在主活動中使SendClientes asynctask保密。或者創建另一個具有SendClientes作爲內部私有類的類,並通過構造函數提供上下文(如果要分離Principal和SendClientes類)。

類似問題: No enclosing instance of the type MainActivity is accessible in scope