简体   繁体   English

尝试制作基本的Webview android应用-启动时崩溃

[英]Trying to make a basic webview android app - crash on startup

I'm trying to make a very basic app, just a webview and nothing else. 我正在尝试制作一个非常基本的应用程序,只是一个webview而已。 I have barely any coding knowledge so I'm very lost. 我几乎没有任何编码知识,所以我很迷路。 I downloaded Android Studio, and followed this tutorial , but whenever I ran it on a virtual device or my GS2 it would give me the "Unfortunately, this app has stopped working" message. 我下载了Android Studio,并按照了本教程的步骤进行操作 ,但是每当我在虚拟设备或GS2上运行它时,都会出现“不幸的是,该应用程序已停止工作”的消息。

Here's all of the code that I've edited from the default template: 这是我从默认模板编辑的所有代码:

http://pastebin.com/MAi4aJR6 http://pastebin.com/MAi4aJR6

Anyone have any ideas why this is happening, or does anyone know of a different tutorial/template I can use? 任何人都知道为什么会发生这种情况,还是有人知道我可以使用其他教程/模板?

As stated in a comment, you need to post the most relevant parts of the code and your logcat here when you get a crash. 如评论中所述,当您崩溃时,您需要在此处发布代码的最相关部分和logcat。 However, this was pretty easy so I will answer it this time. 但是,这很容易,所以我这次会回答。 You are getting a NPE because you are trying to call a method on a variable ( mWebView ) before initializing it 您正在获得NPE因为您试图在变量( mWebView )上对其进行初始化之前对其进行调用

webSettings = mWebView.getSettings();   // here will give a NPE
webSettings.setJavaScriptEnabled(true);
mWebView = (WebView) findViewById(R.id.activity_main_webview);

change that to 改成

mWebView = (WebView) findViewById(R.id.activity_main_webview); //initialize first
webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM