在内部类局部变量的最终的ACCES破 [英] acces of local final variable in inner class broken

查看:78
本文介绍了在内部类局部变量的最终的ACCES破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪一个服务器 - 客户端通信完成的onClick为此我创建一个匿名OnClickListener的方法,我要发布一个敬酒通信是否全成与否。
要做到这一点,我需要的活性的研究中,上下文发布敬酒,和我的外在的方法,它必须被赋予为这个参数的活动。但是作为我是一个匿名内部类中我无法访问活性的研究的这个指针,即使我把它存储在一个局部变量的最终

I got a method in which server-client communication is done "onClick" therefor i create a anonymous OnClickListener, and I want to publish a toast if the communication was successfull or not. To do this I need the Acitivity in which context to publish the toast, and as I externalized the method, it must be given as a "this" argument to the Activity. But as I am inside an anonymous inner class I cannot access the this pointer of the Acitivity, and even though I stored it in a local final variable

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

任何线索什么我搞砸了?

Any clue what i messed up?

推荐答案

在声明变量的的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;
    }

这篇关于在内部类局部变量的最终的ACCES破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆