2016-03-28 7 views
0
abgerufen werden.

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(); 
     } 
    } 

} 
+0

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. –

Antwort

1

Sie müssen die onPageFinished innerhalb der Aktivitätsklasse implementieren, wenn Sie findViewById verwenden möchten.

Wenn Sie keinen Verweis auf das Splash-Bild benötigen, behalten Sie es auch nicht.

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/"); 
+0

dieser Code läuft, aber der Splash-Screen funktioniert nicht so, wie er sollte .....als verknüpfte Frage – baalal

+0

Ich habe dein Gerät nicht, also weiß ich nicht, was du ansiehst, aber wenn das dein Problem mit dem Code nicht gelöst hat, dann schlage ich vor, dass du diese Antwort akzeptierst und einen neuen Beitrag zum Beschreiben verfasst was du mit "wie es soll" meinst –

0

Split diese Linie

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

in zwei getrennte Aussagen

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

Fehler: (78, 9) Fehler: (78, 35) Fehler: kann nicht finden, Symbol Methode findViewById (int) – baalal

0

Sie nicht findViewById mit einer beliebigen Ansicht Kontext verwenden können. Da Sie keine View-Referenz haben.

Sie müssen findViewById init aufrufen oder aufrufen, unabhängig davon, wo Sie unter dem Code anrufen oder initiieren.

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

Initiate this also

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

Ihre Umgreifen Klasse ist kein Activity. Es gibt keine findViewbyId() in WebViewClient Klasse.

+0

, wie man dieses Problem beheben .. eine Idee .. – baalal

+0

Ich habe aktualisiert das nicht Symbolvariable myImageView Fehler finden Frage. bitte prüfe. – baalal

+0

Anstatt MyAppWebViewClient als öffentliche Klasse zu erstellen, sollte dies eine innere Klasse von MainActivity sein. Dann erhalten Sie dort findViewById() ohne weitere Probleme. Übrigens, du musst dich auch danach um @Bob Malooga'r kümmern. –

0

Versuchen:

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

Haben Sie getestet, ob dieser Code funktioniert? – timothymarois

+0

Das kompiliert nicht ... – Floern

Verwandte Themen