2011-09-18 53 views
0

我有一个方法,其中服务器 - 客户端通信完成“onClick”为此,我创建一个匿名OnClickListener,并且我想发布一个吐司,如果通信成功与否。 要做到这一点,我需要在其中上下文来发布敬酒的Acitivity,并且当我将该方法外化时,它必须作为Activity的“this”参数给出。但是作为我是一个匿名内部类中我无法访问胡亚蓉的这个指针,即使我将其存储在本地的最终变量本地最终变量在内部类破碎

private final Activity activity = this; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
      lastResult = null; 
      super.onCreate(savedInstanceState); 
      setLayout(R.layout.main); 
      qrscan = (Button) findViewById(R.id.qrcodescan); 
      qrscan.setOnClickListener(new View.OnClickListener() { 
               public void onClick(View view) { 
                initiateScan(activity); 
               } 
             } 
            ); 
    } 


private AlertDialog initiateSend(Activity activity) { 

     if(lastResult != null) { 
     String[] arr = lastResult.content.split("/"); 
     AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity); 
     String[] args = Util.filterString(arr,this); 
     downloadDialog.setTitle(args[0]); 
     downloadDialog.setMessage("Auftragsnummer:" + args[1]); 
     downloadDialog.setPositiveButton(getString(R.string.ja), new DialogInterface.OnClickListener() { 
                   public void onClick(DialogInterface dialogInterface, int i) { 
                     try { 
                      String send = lastResult.content; 
                      send += "/uid/" + R.id.username + "/cid/" + R.id.password; 
                      String result = Util.send(send); 

                      //toaster(send); 


                      Util.toaster(result,activity); 

                      if(!(result.equals("OK") || result.equals("ok") || result.equals("Ok"))) 
                       throw new Exception("Bad Server Answer"); 



                      Util.toaster("Communication erfolgreich",activity); 
                     } catch(Exception ex) { 
                      ex.printStackTrace(); 
                      Util.toaster("Communication nicht erfolgreich",activity); 
                     } 
                  } 
                 }); 
     downloadDialog.setNegativeButton(getString(R.string.nein), new DialogInterface.OnClickListener() { 
                     public void onClick(DialogInterface arg0, int arg1) {} 
                    }); 
     return downloadDialog.show(); 
     } 
     return null; 
    } 

任何线索什么我搞砸了?

+0

它的工作原理声明变量,如果你在你创建的匿名类中的方法声明 '活动'? –

+0

实际上,编译器不再抱怨了。奇怪的解决方案,但它的工作原理。 – Sim

回答

0

之前的onCreate()这样

public class HelloAndroid extends Activity { 
Activity activity = this; // declare here 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

EDITED

Activity mainActivity; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 

      setLayout(R.layout.main); 
mainActivity = this; 
lastResult = null; 
      qrscan = (Button) findViewById(R.id.qrcodescan); 
      qrscan.setOnClickListener(new View.OnClickListener() { 
               public void onClick(View view) { 
                initiateScan(mainActivity); 
               } 
             } 
            ); 
    } 


private AlertDialog initiateSend(final Activity activity) { 

     if(lastResult != null) { 
     String[] arr = lastResult.content.split("/"); 
     AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity); 
     String[] args = Util.filterString(arr,this); 
     downloadDialog.setTitle(args[0]); 
     downloadDialog.setMessage("Auftragsnummer:" + args[1]); 
     downloadDialog.setPositiveButton(getString(R.string.ja), new DialogInterface.OnClickListener() { 
                   public void onClick(DialogInterface dialogInterface, int i) { 
                     try { 
                      String send = lastResult.content; 
                      send += "/uid/" + R.id.username + "/cid/" + R.id.password; 
                      String result = Util.send(send); 

                      //toaster(send); 


                      Util.toaster(result,activity); 

                      if(!(result.equals("OK") || result.equals("ok") || result.equals("Ok"))) 
                       throw new Exception("Bad Server Answer"); 



                      Util.toaster("Communication erfolgreich",activity); 
                     } catch(Exception ex) { 
                      ex.printStackTrace(); 
                      Util.toaster("Communication nicht erfolgreich",activity); 
                     } 
                  } 
                 }); 
     downloadDialog.setNegativeButton(getString(R.string.nein), new DialogInterface.OnClickListener() { 
                     public void onClick(DialogInterface arg0, int arg1) {} 
                    }); 
     return downloadDialog.show(); 
     } 
     return null; 
    } 
+0

以及它被宣布bevore创建。 – Sim

+0

你能发布完整的代码吗? –

+0

完整的类不会是必需的(而且有点不会),但我可以在更多的上下文中进行编辑。 – Sim