我尝试加载.png图片,然后在我启动应用时,我的WebView加载了网址。这是我的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.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>
当我运行应用程序时,它崩溃并出现以下错误:
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)
参考这一特定代码:
logo.setVisibility(View.VISIBLE);
我在这里缺少什么?我已经将图像分配给了&#39;徽标&#39;变量,并在URL加载过程中设置它。
答案 0 :(得分:2)
您只需在setContentView(R.layout.activity_main);
下方移动徽标分配即可
在此方法调用之后,该视图在内存中不存在。
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); logo = (ImageView) findViewById(R.id.logo);