本文介绍了如何实例化一个自定义的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图创建一个定制的WebView重写一个onScrollChanged方法。但是,当我尝试实例化它,我收到以下错误:
06-13 17:30:09.977:E / AndroidRuntime(20366):致命异常:主要
06-13 17:30:09.977:E / AndroidRuntime(20366):了java.lang.RuntimeException:无法实例活动ComponentInfo {br.com.facilit.target.app.android/br.com.facilit.target.app。 android.BrowserActivity}:显示java.lang.NullPointerException
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.os.Handler.dispatchMessage(Handler.java:99)
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.os.Looper.loop(Looper.java:137)
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.app.ActivityThread.main(ActivityThread.java:4424)
06-13 17:30:09.977:E / AndroidRuntime(20366):在java.lang.reflect.Method.invokeNative(本机方法)
06-13 17:30:09.977:E / AndroidRuntime(20366):在java.lang.reflect.Method.invoke(Method.java:511)
06-13 17:30:09.977:E / AndroidRuntime(20366):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
06-13 17:30:09.977:E / AndroidRuntime(20366):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-13 17:30:09.977:E / AndroidRuntime(20366):在dalvik.system.NativeStart.main(本机方法)
06-13 17:30:09.977:E / AndroidRuntime(20366):致:显示java.lang.NullPointerException
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.content.ContextWrapper.getResources(ContextWrapper.java:81)
。06-13 17:30:09.977:E / AndroidRuntime(20366):在android.view.View<&初始化GT;(View.java:2696)
。06-13 17:30:09.977:E / AndroidRuntime(20366):在android.view.View<&初始化GT;(View.java:2744)
。06-13 17:30:09.977:E / AndroidRuntime(20366):在android.view.ViewGroup<&初始化GT;(ViewGroup.java:385)
。06-13 17:30:09.977:E / AndroidRuntime(20366):在android.widget.AbsoluteLayout<&初始化GT;(AbsoluteLayout.java:52)
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.webkit.WebView<&初始化GT;(WebView.java:1119)
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.webkit.WebView<&初始化GT;(WebView.java:1102)
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.webkit.WebView<&初始化GT;(WebView.java:1091)
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.webkit.WebView<&初始化GT;(WebView.java:1081)
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.webkit.WebView<&初始化GT;(WebView.java:1072)
。06-13 17:30:09.977:E / AndroidRuntime(20366):在br.com.facilit.target.app.android.CustomizedWebView<&初始化GT;(CustomizedWebView.java:12)
。06-13 17:30:09.977:E / AndroidRuntime(20366):在br.com.facilit.target.app.android.BrowserActivity<&初始化GT;(BrowserActivity.java:29)
06-13 17:30:09.977:E / AndroidRuntime(20366):在java.lang.Class.newInstanceImpl(本机方法)
06-13 17:30:09.977:E / AndroidRuntime(20366):在java.lang.Class.newInstance(Class.java:1319)
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.app.Instrumentation.newActivity(Instrumentation.java:1023)
06-13 17:30:09.977:E / AndroidRuntime(20366):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
06-13 17:30:09.977:E / AndroidRuntime(20366):... 11个
我的定制的WebView code:
公共类CustomizedWebView扩展的WebView { 公共CustomizedWebView(上下文的背景下){
超级(上下文);
} 公共CustomizedWebView(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
} @覆盖
保护无效onScrollChanged(int类型l,诠释T,INT oldl,诠释oldt){ Log.d(Constants.BROWSER_ACTIVITY,更改过);
super.onScrollChanged(L,T,oldl,oldt);
}
}
我如何实例吧:
公共类BrowserActivity延伸活动{ 私人MetaDados metaDado;
字符串URL;
CustomizedWebView web视图=新CustomizedWebView(本);
进度progressUrl; @覆盖
保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_browser); 最后的动作条动作条= getActionBar();
actionBar.setDisplayHomeAsUpEnabled(真); progressUrl =(进度)findViewById(R.id.progressUrl);
捆绑额外= getIntent()getExtras()。 如果(临时演员!= NULL){ metaDado = extras.getParcelable(metaDado);
} TextView的browserTitle =(的TextView)findViewById(R.id.browser_title); browserTitle.setText(metaDado.getName()); web视图=(CustomizedWebView)findViewById(R.id.webView1); webView.getSettings()setJavaScriptEnabled(真)。
。webView.getSettings()setBuiltInZoomControls(真);
。webView.getSettings()setSupportZoom(真);
。webView.getSettings()setUseWideViewPort(真);
。webView.getSettings()setLoadWithOverviewMode(真); 如果(metaDado.getType()。等于(Constants.FULL)){ URL =文件://+ Constants.DEST_PATH_PARENT +/+ metaDado.getUuidInstituicao()+/全/
+ metaDado.getOfflineUuid()+/index.htm」明明; }否则如果(metaDado.getType()。等于(常量。preS)){ URL =文件://+ Constants.DEST_PATH_PARENT +/+ metaDado.getUuidInstituicao()+/ preS /
+ metaDado.getOfflineUuid()+/+ metaDado.getOfflineUuid()+/index.htm」明明;
} webView.setWebViewClient(新WebViewClient(){ @覆盖
公共无效onPageStarted(的WebView视图,字符串URL,位图图标){ progressUrl.setVisibility(View.VISIBLE); super.onPageStarted(查看,网址,图标);
} @覆盖
公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL){ view.loadUrl(URL);
返回true;
} @覆盖
公共无效onPageFinished(的WebView视图,字符串URL){
super.onPageFinished(查看,网址); progressUrl.setVisibility(View.GONE);
} }); webView.loadUrl(URL); addListeners();
} 私人无效addListeners(){ 按钮GoBack的=(按钮)findViewById(R.id.back); 按钮goForward =(按钮)findViewById(R.id.forward); goForward.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){ 如果(webView.canGoForward()){ webView.goForward();
}
}
}); goBack.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){ 如果(webView.canGoBack()){ webView.goBack();
}
}
}); }}
我的xml文件:
< br.com.facilit.target.app.android.CustomizedWebView
机器人:ID =@ + ID / webView1
机器人:layout_below =@ ID /头
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:可点击=真
/>
解决方案
删除
CustomizedWebView web视图=新CustomizedWebView(本);
由于这似乎是不需要的,或移动新CustomizedWebView(本)
在的onCreate(savedInstanceState)被称为
您上下文不充分的活动类的初始化期间初始化
I'm trying to create a customized WebView that overrides a onScrollChanged method. But when I try to instantiate it I get the following error:
06-13 17:30:09.977: E/AndroidRuntime(20366): FATAL EXCEPTION: main
06-13 17:30:09.977: E/AndroidRuntime(20366): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{br.com.facilit.target.app.android/br.com.facilit.target.app.android.BrowserActivity}: java.lang.NullPointerException
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.os.Looper.loop(Looper.java:137)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-13 17:30:09.977: E/AndroidRuntime(20366): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 17:30:09.977: E/AndroidRuntime(20366): at java.lang.reflect.Method.invoke(Method.java:511)
06-13 17:30:09.977: E/AndroidRuntime(20366): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-13 17:30:09.977: E/AndroidRuntime(20366): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-13 17:30:09.977: E/AndroidRuntime(20366): at dalvik.system.NativeStart.main(Native Method)
06-13 17:30:09.977: E/AndroidRuntime(20366): Caused by: java.lang.NullPointerException
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.view.View.<init>(View.java:2696)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.view.View.<init>(View.java:2744)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.view.ViewGroup.<init>(ViewGroup.java:385)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:52)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.webkit.WebView.<init>(WebView.java:1119)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.webkit.WebView.<init>(WebView.java:1102)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.webkit.WebView.<init>(WebView.java:1091)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.webkit.WebView.<init>(WebView.java:1081)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.webkit.WebView.<init>(WebView.java:1072)
06-13 17:30:09.977: E/AndroidRuntime(20366): at br.com.facilit.target.app.android.CustomizedWebView.<init>(CustomizedWebView.java:12)
06-13 17:30:09.977: E/AndroidRuntime(20366): at br.com.facilit.target.app.android.BrowserActivity.<init>(BrowserActivity.java:29)
06-13 17:30:09.977: E/AndroidRuntime(20366): at java.lang.Class.newInstanceImpl(Native Method)
06-13 17:30:09.977: E/AndroidRuntime(20366): at java.lang.Class.newInstance(Class.java:1319)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
06-13 17:30:09.977: E/AndroidRuntime(20366): ... 11 more
My customized webview code:
public class CustomizedWebView extends WebView {
public CustomizedWebView(Context context) {
super(context);
}
public CustomizedWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Log.d(Constants.BROWSER_ACTIVITY, "SCROLL CHANGED");
super.onScrollChanged(l, t, oldl, oldt);
}
}
How do I instantiate it:
public class BrowserActivity extends Activity {
private MetaDados metaDado;
String url;
CustomizedWebView webView = new CustomizedWebView(this);
ProgressBar progressUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
progressUrl = (ProgressBar) findViewById(R.id.progressUrl);
Bundle extras = getIntent().getExtras();
if (extras != null) {
metaDado = extras.getParcelable("metaDado");
}
TextView browserTitle = (TextView) findViewById(R.id.browser_title);
browserTitle.setText(metaDado.getName());
webView = (CustomizedWebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
if (metaDado.getType().equals(Constants.FULL)) {
url = "file://" + Constants.DEST_PATH_PARENT + "/" + metaDado.getUuidInstituicao() + "/full/"
+ metaDado.getOfflineUuid() + "/index.html";
} else if (metaDado.getType().equals(Constants.PRES)) {
url = "file://" + Constants.DEST_PATH_PARENT + "/" + metaDado.getUuidInstituicao() + "/pres/"
+ metaDado.getOfflineUuid() + "/" + metaDado.getOfflineUuid() + "/index.html";
}
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressUrl.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressUrl.setVisibility(View.GONE);
}
});
webView.loadUrl(url);
addListeners();
}
private void addListeners() {
Button goBack = (Button) findViewById(R.id.back);
Button goForward = (Button) findViewById(R.id.forward);
goForward.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (webView.canGoForward()) {
webView.goForward();
}
}
});
goBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (webView.canGoBack()) {
webView.goBack();
}
}
});
}
}
My xml file:
<br.com.facilit.target.app.android.CustomizedWebView
android:id="@+id/webView1"
android:layout_below="@id/header"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
/>
解决方案
Remove
CustomizedWebView webView = new CustomizedWebView(this);
As it appear to be unrequired, or move the new CustomizedWebView(this)
to be called in onCreate(savedInstanceState)
Your context isn't fully initialised during the initialisation of the activity class
这篇关于如何实例化一个自定义的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!