Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓:绕过';无法在定义的内部类内引用非最终变量';_Android_Eclipse_Web Services - Fatal编程技术网

Android 安卓:绕过';无法在定义的内部类内引用非最终变量';

Android 安卓:绕过';无法在定义的内部类内引用非最终变量';,android,eclipse,web-services,Android,Eclipse,Web Services,尝试将此内容输入android文本框,我使用以下代码: utton btngetUsers = (Button) findViewById(R.id.button2); btngetUsers.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stu

尝试将此内容输入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变量

尝试在其他函数中使用字符串行时
如何绕过这个

错误在于,在JAVA中,除非变量在方法中声明为final,或者变量本身是父类的全局变量,否则无法从方法访问该方法中的匿名类的变量

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

字符串行
声明为全局变量

再想一想,我认为您更愿意显示
stringBuffer
而不是
,因为后者只是临时读取
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
}

尝试将您的
变量设置为类的字段…@MateusBrandao请解释更多,我是初学者
private String line=null
。我是初学者,请解释更多或查看代码示例。你想在文本视图上显示整个阅读页面,对吗?试试我发布的解决方案