Java 在WebView上加载URL之前无法加载图像
当我启动应用程序时,我正在尝试在WebView加载URL之前加载.png图像。以下是我的Java代码:Java 在WebView上加载URL之前无法加载图像,java,android,webview,nullpointerexception,imageview,Java,Android,Webview,Nullpointerexception,Imageview,当我启动应用程序时,我正在尝试在WebView加载URL之前加载.png图像。以下是我的Java代码: 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.webk
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);
}
}
});
}
}
我的布局文件如下:
<?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>
关于这一特定代码行:
logo.setVisibility(View.VISIBLE);
我错过了什么?我已将图像分配给“logo”变量,并正在URL加载过程中对其进行设置。您只需将徽标分配移动到
setContentView(R.layout.activity\u main)下方即可代码>
在调用此方法之前,该视图不存在于内存中
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logo = (ImageView) findViewById(R.id.logo);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logo=(ImageView)findviewbyd(R.id.logo);
在使用findViewById开始抓取视图之前,必须始终设置内容视图
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logo = (ImageView) findViewById(R.id.logo);