无法通过id获取ImageView

时间:2016-03-28 17:55:37

标签: java android webview

我希望在应用完成加载网址之前显示启动画面 搜索后,我找到了一个解决方案 - Splash screen while loading a url in a webview in android app

但是我无法在我当前的代码上实现它。我在尝试通过id获取启动图像视图时收到错误消息。

这是我的代码:

public class MyAppWebViewClient extends WebViewClient {

    private final Context context;

    public MyAppWebViewClient(Context context) {
        this.context = context;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (url.contains("?dl=1")) {
            Uri source = Uri.parse(url);
            // Make a new request pointing to the download url
            DownloadManager.Request request = new DownloadManager.Request(source);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            }

            String fileExtension = MimeTypeMap.getFileExtensionFromUrl(url);
            String fileName = URLUtil.guessFileName(url, null, fileExtension);

            // save the file in the "Downloads" folder of SDCARD
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
            // get download service and enqueue file
            DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            manager.enqueue(request);
            return true;
        }



        if (Uri.parse(url).getHost().contains("www.example.com")) {
            return false;
        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        view.loadUrl("file:///android_asset/offline.html");
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        //hide loading image
        myImageView = (ImageView) findViewById(R.id.activity_splash_logo).setVisibility(View.GONE);
        //show webview
        view.findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
    }

}

和activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myapp.MainActivity">

    <WebView
        android:id="@+id/activity_main_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>
    <ImageView
        android:id="@+id/activity_splash_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/splash_logo"
        android:visibility="visible"
        />
</RelativeLayout>

这一行出现了问题:

myImageView = (ImageView) findViewById(R.id.activity_splash_logo).setVisibility(View.GONE);

mainactivity.java

package com.myapp;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_WRITE_EXTERNAL_STORAGE = 0;
    private WebView mWebView;

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

        mWebView = (WebView) findViewById(R.id.activity_main_webview);

        // Stop local links and redirects from opening in browser instead of WebView
        mWebView.setWebViewClient(new MyAppWebViewClient(MainActivity.this));

        mWebView.loadUrl("http://www.example.com/");

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);


        // Request required permission
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_WRITE_EXTERNAL_STORAGE);
        }

        // Add user agent suffix
        this.mWebView.getSettings().setUserAgentString(
                this.mWebView.getSettings().getUserAgentString()
                        + " "
                        + BuildConfig.APPLICATION_ID
                        + "/"
                        + BuildConfig.VERSION_CODE
        );
    }

    // Prevent the back-button from closing the app
    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

}

5 个答案:

答案 0 :(得分:1)

您所包含的课程不是ActivityfindViewbyId()类中没有WebViewClient

答案 1 :(得分:1)

如果要使用[...],则需要在Activity类中实现onPageFinished

此外,如果您不需要对启动图像的引用,请不要保留它。

findViewById

答案 2 :(得分:0)

拆分此行

class Person{
  var name: String
  var address: Address

  init(name: String, address: Address){
    self.name = name
    self.address = address
  }

}

class Address {
  var city: String
  var country: String
  init(city: String, country: String){
    self.city = city
    self.country = country
  }
}


var address = Address(city: "New York", country: "USA")

    var person1 = Person(name: "John", address: address)

    var person2 = Person(name: "Adam", address: address)

    person1.address.city = "Washington"

    print(person1.address.city)

    print(person2.address.city)

分为两个单独的陈述

myImageView = (ImageView) findViewById(R.id.activity_splash_logo).setVisibility(View.GONE);

答案 3 :(得分:0)

您不能将findViewById用于任何视图上下文。因为你没有任何View参考。

无论您在何处呼叫或启动以下代码,您都必须初始化或调用findViewById。

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
  

也启动这个

myImageView = (ImageView) findViewById(R.id.activity_splash_logo);

答案 4 :(得分:0)

尝试:

ImageView myImageView = 
        (ImageView) findViewById(R.id.activity_splash_logo).setVisibility(View.GONE);