Android:绕过'无法引用定义的内部类中的非final变量'

时间:2016-01-29 12:56:20

标签: android eclipse web-services

尝试将此site中的内容导入到android文本框中,这是我使用的代码:

utton btngetUsers = (Button) findViewById(R.id.button2);
    btngetUsers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        URL hellofileurl = new URL(
                                "http://saryelgmal.esy.es/getuser.php");
                        HttpURLConnection HelloWorldCon = (HttpURLConnection) hellofileurl
                                .openConnection();
                        InputStreamReader stream = new InputStreamReader(
                                HelloWorldCon.getInputStream());
                        BufferedReader ourstreamreader = new BufferedReader(
                                stream);

                        // buffer for storing file contents in memory
                        StringBuffer stringBuffer = new StringBuffer("");
                        // for reading one line
                        String line = null;
                        // keep reading till readLine returns null
                        while ((line = ourstreamreader.readLine()) != null) {
                            // keep appending last line read to buffer
                            stringBuffer.append(line);
                        }
                        runOnUiThread(new Runnable() {
                            public void run() {
                                text.setText(line);
                            }
                        });
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            Thread thread = new Thread(runnable);
            thread.start();
        }
    });

我怎么得到错误

  

不能引用内部类定义的非final变量

尝试在另一个函数中使用字符串行时 如何绕过这个!!

1 个答案:

答案 0 :(得分:0)

在JAVA中,除了变量在方法内声明为final或者变量本身是父类的全局变量之外,你无法在方法中访问变量从方法到匿名类。

这里你的变量行是用public void run()方法声明的,你试图在new Runnable()匿名内部类中使用它。

声明您的String line是全局变量。

第二个想法,我认为你更愿意显示stringBuffer而不是line,因为后者只是暂时读取ourstreamreader的一部分。因此line会不断变化,但您的stringBuffer包含全部内容。无论如何,如果你想以类似的逻辑显示stringBuffer,则声明stringBuffer是该类的全局。

试试这段代码:

public class MainActivity extends AppCompatActivity {

    //Declare the stringBuffer variable to be global here
    StringBuffer stringBuffer;
    String line;

    @Override
    public void onCreate(Bundle savedInstanceState){
        //Inflate layouts and initialise view objects

        //This is from where you posted
        Button btngetUsers = (Button) findViewById(R.id.button2);
        btngetUsers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        try {
                            URL hellofileurl = new URL(
                                    "http://saryelgmal.esy.es/getuser.php");
                            HttpURLConnection HelloWorldCon = (HttpURLConnection) hellofileurl
                                    .openConnection();
                            InputStreamReader stream = new InputStreamReader(
                                    HelloWorldCon.getInputStream());
                            BufferedReader ourstreamreader = new BufferedReader(
                                    stream);

                            // buffer for storing file contents in memory
                            stringBuffer = new StringBuffer("");
                            // for reading one line
                            line = null;
                            // keep reading till readLine returns null
                            while ((line = ourstreamreader.readLine()) != null) {
                                // keep appending last line read to buffer
                                stringBuffer.append(line);
                            }
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    //Display stringBuffer, not line
                                    text.setText(stringBuffer);
                                }
                            });
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                };
                Thread thread = new Thread(runnable);
                thread.start();
            }
        });
        //Other things you need to do in onCreate()
    }
    //Other methods
}