[英]Unable to get ImageView by id
I want to show a splash screen until the app finishes loading the URL. 我想显示一个初始屏幕,直到应用程序完成URL的加载为止。
After searching, I found a solution - Splash screen while loading a url in a webview in android app 搜索后,我找到了解决方法- 在Android应用的Webview中加载网址时启动屏幕
But I'm unable to implement it on my current code. 但是我无法在当前代码中实现它。 I'm getting an error message while trying to get the splash image view by id.
尝试通过id获取启动画面视图时出现错误消息。
Here is my code: 这是我的代码:
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);
}
}
and activity.xml 和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>
The problem occuring in this line : 此行中发生的问题:
myImageView = (ImageView) findViewById(R.id.activity_splash_logo).setVisibility(View.GONE);
mainactivity.java 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();
}
}
}
Your encompassing class is not a Activity
. 您的上课不是
Activity
。 There is no findViewbyId()
in WebViewClient
class. WebViewClient
类中没有findViewbyId()
。
You need to implement the onPageFinished
within the Activity class if you want to use findViewById
. 如果要使用
findViewById
则需要在Activity类内实现onPageFinished
。
Also, if you don't need a reference to the splash image, then don't keep one. 另外,如果您不需要对初始图像的引用,则不要保留该图像。
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/");
Split this line 分割这条线
myImageView = (ImageView) findViewById(R.id.activity_splash_logo).setVisibility(View.GONE);
into two separate statements 分为两个独立的陈述
myImageView = (ImageView) findViewById(R.id.activity_splash_logo);
myImageView.setVisibility(View.GONE);
You can't use findViewById with any view context. 您不能在任何视图上下文中使用findViewById。 As you don't have any View reference.
由于您没有任何View参考。
You must init or call findViewById, wherever you are calling or initiating below code. 无论您在下面的代码中调用或初始化的地方,都必须初始化或调用findViewById。
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
Initiate this also
也开始这个
myImageView = (ImageView) findViewById(R.id.activity_splash_logo);
Try: 尝试:
ImageView myImageView =
(ImageView) findViewById(R.id.activity_splash_logo).setVisibility(View.GONE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.