Ich möchte einen Startbildschirm anzeigen, bis die App die URL geladen hat.
Nach der Suche fand ich eine Lösung - Splash screen while loading a url in a webview in android appImageView konnte nicht mit der ID
Aber ich bin nicht in der Lage, es auf meinem aktuellen Code zu implementieren. Ich erhalte eine Fehlermeldung beim Versuch, die Splash-Bildansicht nach ID zu erhalten.
Hier ist mein 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);
}
}
und 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>
Das Problem in dieser Linie auftreten:
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();
}
}
}
Der Beitrag, mit dem Sie verbunden haben, hat Code innerhalb einer 'Aktivität', wo Sie' findViewById' aufrufen können. Ihr Code ist hier in einem 'WebViewClient', so dass Sie' findViewById' nicht aufrufen können, da dies keine Methode dieser Klasse ist. –