Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.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
Java 无法按id获取ImageView_Java_Android_Webview - Fatal编程技术网

Java 无法按id获取ImageView

Java 无法按id获取ImageView,java,android,webview,Java,Android,Webview,我想显示一个启动屏幕,直到应用程序加载完URL。 搜索之后,我找到了一个解决方案- 但我无法在当前代码中实现它。尝试按id获取启动图像视图时收到错误消息 这是我的密码: public class MyAppWebViewClient extends WebViewClient { private final Context context; public MyAppWebViewClient(Context context) { this.context =

我想显示一个启动屏幕,直到应用程序加载完URL。
搜索之后,我找到了一个解决方案-

但我无法在当前代码中实现它。尝试按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>
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();
        }
    }

}
分开这条线

myImageView = (ImageView) findViewById(R.id.activity_splash_logo).setVisibility(View.GONE);
分为两个单独的语句

myImageView = (ImageView) findViewById(R.id.activity_splash_logo);
myImageView.setVisibility(View.GONE);
分开这条线

myImageView = (ImageView) findViewById(R.id.activity_splash_logo).setVisibility(View.GONE);
分为两个单独的语句

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

不能将findViewById用于任何视图上下文。因为您没有任何视图引用

无论在何处调用或启动下面的代码,都必须初始化或调用findViewById

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
也发起这个


不能将findViewById用于任何视图上下文。因为您没有任何视图引用

无论在何处调用或启动下面的代码,都必须初始化或调用findViewById

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
也发起这个


您的包含类不是
活动
WebViewClient
类中没有
findViewbyId()

您的包含类不是
活动。
WebViewClient
类中没有
findViewbyId()

请尝试:

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


如果要使用
findViewById
,则需要在Activity类中实现
onPageFinished

此外,如果您不需要对飞溅图像的引用,那么不要保留一个

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

mWebView.setWebViewClient(new MyAppWebViewClient(MainActivity.this) {

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

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

如果要使用
findViewById
,则需要在Activity类中实现
onPageFinished

此外,如果您不需要对飞溅图像的引用,那么不要保留一个

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

mWebView.setWebViewClient(new MyAppWebViewClient(MainActivity.this) {

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

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

错误:(78,9)错误:找不到符号变量myImageView错误:(78,35)错误:找不到符号方法findViewById(int)错误:(78,9)错误:找不到符号变量myImageView错误:(78,35)错误:找不到符号方法findViewById(int)链接到的帖子在
活动中有代码,您可以在其中调用
findViewById
。此处的代码位于
WebViewClient
中,因此您不能调用
findViewById
,因为这不是该类的方法。您链接到的帖子在
活动
中包含代码,您可以在其中调用
findViewById
。此处的代码位于
WebViewClient
中,因此不能调用
findViewById
,因为这不是该类的方法。与其将MyAppWebViewClient设置为公共类,不如将其设置为MainActivity的内部类。然后您将在那里获得findViewById(),而不会出现任何其他问题。顺便说一句,在那之后你还需要关心@Bob Malooga'r的情况。与其将MyAppWebViewClient作为公共类,不如将其作为MainActivity的内部类。然后您将在那里获得findViewById(),而不会出现任何其他问题。顺便说一句,在那之后你还需要关注@Bob Malooga'r案例。此代码正在运行,但启动屏幕没有正常工作。。。。。作为链接问题,我没有你的设备,所以我不知道你在看什么,但是如果这解决了你的代码没有运行的问题,那么我建议你接受这个答案,并发表一篇新的帖子来描述你所说的“应该”是什么意思。这个代码正在运行,但是启动屏幕没有正常工作。。。。。作为链接问题,我没有你的设备,所以我不知道你在看什么,但如果这解决了你的代码不运行的问题,那么我建议你接受这个答案,并写一篇新的帖子来描述你所说的“应该”是什么意思