变量视图可能没有从内部视图访问初始化和变量视图

时间:2017-08-05 09:27:35

标签: java android webview android-webview

我经历过很多帖子和论坛但却找不到解决方案。我正在开发一个带有本地html文件的应用程序,我有这个主要的活动

package com.faraksoch.sagar.eroutine;



public class MainActivity extends AppCompatActivity {
boolean doubleBackToExitPressed = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView webView = (WebView) findViewById(webView);


    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setAllowFileAccess(true);


    webView.setWebViewClient(new WebViewClient() {


        public void onPageFinished(WebView view, String url) {
            findViewById(R.id.image1).setVisibility((View.GONE));
            findViewById(webView).setVisibility(View.VISIBLE);


        }
    });


    webView.loadUrl("file:///android_asset/index.html");

    webView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        }
    });

    Intent intent = new Intent(MainActivity.this, UpdateService.class);
    startService(intent);
}

boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Please click BACK again to exit",
            Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
        }
    }, 2000);


}
WebView webView;
public void goBack(){
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        finish();
    }
}

}

在这里排队

WebView webView = (WebView) findViewById(webView);

我收到的webView错误可能尚未初始化并且在线

findViewById(webView).setVisibility(View.VISIBLE);

变量webView是从内部类访问的,需要声明为final,因此当我将代码更改为

 final WebView webView = (WebView) findViewById(webView);

仍然没有运气。

我的Main_activity文件是

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.faraksoch.sagar.eroutine.MainActivity">

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"
     />


<ImageView
    android:id="@+id/image1"
    android:layout_width="368dp"
    android:layout_height="576dp"
    android:background="@drawable/ssplash" />

</android.support.constraint.ConstraintLayout>

任何人都可以帮助我,我必须做些什么才能让它发挥作用。

2 个答案:

答案 0 :(得分:0)

您似乎在主类中声明了WebView类型的变量,就在goBack()函数之前。在该行:

WebView webView = (WebView) findViewById(webView);

声明并初始化一个新的局部变量,该值在onCreate结束时丢失。此外,参数webView是一个未在代码中初始化的类成员,这就是编译器警告你的原因。

请注意,在您的匿名内部类中,webView引用类成员,而不是局部变量,因此您必须正确初始化它并使其成为final

答案 1 :(得分:0)

findViewById takes an integer, in your line,

WebView webView = (WebView) findViewById(webView);

What is the webView where have you initialized this variable, you should have initialized like int webView= R.id.webView;

You are doing the same mistale here findViewById(webView).setVisibility(View.VISIBLE);

Initialize the webview like WebView webView = (WebView )findViewById(R.id.your_web_view); and then use it.