内部类中的本地最终变量的访问被破坏

时间:2011-09-18 12:07:46

标签: android

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

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

我弄乱了什么线索?

1 个答案:

答案 0 :(得分: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;
    }