So I'm working on adding some web view stuff and will eventually make this more complicated but for now am just trying to get the basics working. I have my xml built and such and I am inflating it on top of another view. So its in like a dialog box.
web = (WebView)findViewById(R.id.WebView);
name = (TextView) findViewById(R.id.name);
name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(Hydrogen.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.web);
dialog.setCancelable(true);
web.loadUrl("http://en.wikipedia.org/wiki/" + name.getText().toString());
RelativeLayout btn = (RelativeLayout) dialog.findViewById(R.id.close);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}});
Basically thats my code regarding the WebView etc. The problem is on line 145 which is the web.loadUrl line. I'n not sure whats wrong but every time I try to open the Dialog it crashs. Thanks.
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): FATAL EXCEPTION: main
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): java.lang.NullPointerException
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): at table.periodic.Hydrogen$1.onClick(Hydrogen.java:145)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): at android.view.View.performClick(View.java:2408)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): at android.view.View$PerformClick.run(View.java:8816)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): at android.os.Handler.handleCallback(Handler.java:587)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): at android.os.Looper.loop(Looper.java:123)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): at java.lang.reflect.Method.invoke(Method.java:521)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): at dalvik.system.NativeStart.main(Native Meth开发者_如何转开发od)
I also just tried making a seperate project in which to load the web page. Just a WebView loading the URL. It partly worked. It worked to the point that it opened the URL but it did it inside the browser instead of inside the webview. Could be because I named the WebView browser but will try more.
change
btn.setOnClickListener(new View.OnClickListener() {
for
btn.setOnClickListener(new OnClickListener() {
Throw in a breakpoint and make sure that your webview is not null when you try attaching it. The only way it would be crashing is if your webview is null. Make sure the name reflects the component webview that you are refering to in your xml layout file.
So I found it out myself. My dialog code had to looks as follows.
name = (TextView) findViewById(R.id.name);
name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(Hydrogen.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.web);
dialog.setCancelable(true);
eleweb = (WebView) dialog.findViewById(R.id.eleweb);
eleweb.loadUrl("http://en.wikipedia.org/wiki/" + name.getText().toString());
eleweb.setWebViewClient(new HelloWebViewClient());
RelativeLayout btn = (RelativeLayout) dialog.findViewById(R.id.close);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}});
the problem what that I wasn't calling the WebView in a dialog format. Once I got all the WebView information loading inside the Dialog and did
eleweb = (WebView) dialog.findViewById(R.id.eleweb);
it worked.
精彩评论