user1650978 user1650978 - 2 days ago 5
Java Question

Unable to load image before loading URL on WebView

I'm trying to load a .png image before my WebView loads the URL when I start my app. Here is my Java code for the same :

package course.org.mywebsite;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ImageView;

public class MainActivity extends Activity {


WebView mWebView;
ImageView logo;

@Override
public void onCreate(Bundle savedInstanceState)
{
final Activity mActivity = this;
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
super.onCreate(savedInstanceState);

logo = (ImageView) findViewById(R.id.logo);
setContentView(R.layout.activity_main);

getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

mWebView = (WebView) findViewById( R.id.webview );
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(MyURLHere);

mWebView.setWebChromeClient(new WebChromeClient() {

public void onProgressChanged(WebView view, int progress) {
//Make the bar disappear after URL is loaded, and changes string to Loading...
mActivity.setTitle("Loading...");
mActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

logo.setVisibility(View.VISIBLE);


if(progress == 100) {
mActivity.setTitle(R.string.app_name);
logo.setVisibility(View.GONE);

}
}


});


}
}


My layout file is as follows :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="invisible"
android:src="@drawable/logo"/>

<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>


When I run the app, it crashes with the following error:

FATAL EXCEPTION: main
Process: course.org.mywebsite, PID: 22730

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setVisibility(int)' on a null object reference
at course.org.sensoredlife.MainActivity$1.onProgressChanged(MainActivity.java:52)
at com.android.webview.chromium.WebViewContentsClientAdapter.onProgressChanged(WebViewContentsClientAdapter.java:239)
at org.chromium.android_webview.AwContentsClientCallbackHelper$MyHandler.handleMessage(AwContentsClientCallbackHelper.java:169)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


Referring to this particular line of code :

logo.setVisibility(View.VISIBLE);


What am I missing here? I have already assigned the image to the 'logo' variable and am setting it during progress of the URL load.

Answer

You should just need to move your logo assignment below setContentView(R.layout.activity_main); The view doesn't exist in memory until after this method call.


    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    logo = (ImageView) findViewById(R.id.logo);

Comments