Error when referencing global variable

Asked

Viewed 102 times

0

I have an application in which you have a webview, so I created a preloader to load into a thread , now I need to put the webview in another thread , how can I do (code error ) ?

import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.net.ConnectivityManager;
import android.content.Context;
import android.net.NetworkInfo;
import android.net.Network;
import android.widget.ProgressBar;

public class ConectActivity extends Activity {


    private WebView webView;
    private ProgressBar progress;

    public  boolean verificaConexao() {
        boolean conectado;
        ConnectivityManager conectivtyManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (conectivtyManager.getActiveNetworkInfo() != null
                && conectivtyManager.getActiveNetworkInfo().isAvailable()
                && conectivtyManager.getActiveNetworkInfo().isConnected()) {
            conectado = true;
        } else {
            conectado = false;
        }
        return conectado;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conect);
        // *** roda qnd abre - Augusto Furlan ***
        Boolean conect = verificaConexao();
        String url = "http://google.com";
        webView = (WebView) findViewById(R.id.webView);
        progress = (ProgressBar) findViewById(R.id.progress);
        //webView.setWebViewClient(new CustomWebViewClient());
        webView.setVisibility(webView.GONE);

        WebSettings ws = webView.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(true);
        webView.setWebViewClient(new WebViewClient());



        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(25000);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showWebView();
                    }
                });
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                if(self.conect == true) {
                    webView.loadUrl(self.url);
                } else {webView.loadUrl("file:///android_asset/not-found.html");;}

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showWebView();
                    }
                });
            }
        }).start();
        }



    private void showWebView() {
        webView.setVisibility(View.VISIBLE);
        progress.setVisibility(View.GONE);
    }

}
  • Why didn’t you put the code here instead of using Pastebin?

  • because it was already there , for being more practical, but I can put the code here, 1 min

  • What is the mistake? I suggest that Edit your question and highlight what the error is.

  • at the time within the second thread it does not allow me to use self.url , does not allow access to global

  • But not everyone who can help you always has access to those links. It is recommended to always post the source of the error right here and, for long codes, post on the external link for those who want more details of your code.

  • OK sorry, already edited and put the code

  • I have some variables and I need to access to perform the comparison

  • See if my answer answers to what you want.

Show 3 more comments

1 answer

0

If you want to access the attributes conect and url class Conectactivity shall do so directly using their names.
The word selfdoes not exist in JAVA.

So that these attributes can be used in a inner class need, however, to be declared as final:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_conect);

    final Boolean conect = verificaConexao();
    final String url = "http://google.com";

    ...
    ...
    new Thread(new Runnable() {
        @Override
        public void run() {
            if(conect == true) {
                webView.loadUrl(url);
            } else {webView.loadUrl("file:///android_asset/not-found.html");;}

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showWebView();
                }
            });
        }
    }).start();
}
  • friend do not know why put final after the type in the statement, but still did not work and accuses error

  • When it’s wrong, it’d be nice to say what it is. :)

  • Error:(77, 24) error: cannot find Symbol variable conect Error:(78, 41) error: cannot find Symbol variable url

  • This variable you want to use is the attribute url class Conectactivity?

  • yes, like this in the code, but this inside the function protected void onCreate(Bundle savedInstanceState) {

  • Where you think the problem is ?

  • Try declaring conect and url at class level, outside the method onCreate()

  • error in the code stopped, but now when it opens it accuses that the app has stopped

  • That will be for another reason. However I do not understand why it gives error being the statement within onCreate().

  • what you think it might be ?

  • Without knowing what the error is I find nothing. This being another problem should create new question where to post the Logcat with the error.

  • FATAL EXCEPTION: main

  • I did not know whether or not my answer solved the problem originally posed in the question.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.