android


Webview crashing android app on startup


I'm using webview in an android app and I'm trying to get it to start automatically on the second time the app is started after saving the settings. After setting breakpoints I was able to determine the app is crashing on line "mWebView.setWebChromeClient(new WebChromeClient()" in WebActivity. I get the error source not found in debugger.
WebActivity:
public class WebActivity extends Activity
{
WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final Activity mActivity = this;
// Adds Progress bar Support
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_web);
// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById( R.id.webview );
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
findViewById(R.id.webview).setVisibility(View.GONE);
mActivity.setTitle("Loading...");
mActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if(progress == 100)
{
mActivity.setTitle(R.string.webtitle);
findViewById(R.id.webview).setVisibility(View.VISIBLE);
}
}
});
Bundle b = getIntent().getExtras();
mWebView.loadUrl("http://" + (b.getString("iptextfield")) + "/index.html");
WebView wv = (WebView) findViewById(R.id.webview);
wv.setWebViewClient(new WebViewClient()
{
boolean toast_f = true;
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
Context context = getApplicationContext();
CharSequence text = "Loading...(this may take up to 60 seconds)";
if (toast_f)
{
toast_f = false;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.CENTER|Gravity.CENTER,0,0);
toast.show();
}
Bundle b = getIntent().getExtras();
mWebView.loadUrl("http://" + (b.getString("iptextfield")) + "/index.html");
}
});
}
}
Main activity:
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent;
SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
String GetStatus = sharedPref.getString("DataSaved", "");
if (GetStatus.equals("true"))
{
intent = new Intent(this, WebActivity.class);
}
else
{
intent = new Intent(this, SetupActivity.class);
}
startActivity(intent);
finish();
}
}
Here's the logcat errors I get when just running the app:
08-19 12:25:54.407: W/dalvikvm(15158): threadid=1: thread exiting with uncaught exception (group=0x4202d438)
08-19 12:25:54.407: E/AndroidRuntime(15158): FATAL EXCEPTION: main
08-19 12:25:54.407: E/AndroidRuntime(15158): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.webpage.WebActivity}: java.lang.NullPointerException
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2098)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.ActivityThread.access$600(ActivityThread.java:138)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.os.Looper.loop(Looper.java:137)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.ActivityThread.main(ActivityThread.java:4911)
08-19 12:25:54.407: E/AndroidRuntime(15158): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 12:25:54.407: E/AndroidRuntime(15158): at java.lang.reflect.Method.invoke(Method.java:511)
08-19 12:25:54.407: E/AndroidRuntime(15158): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-19 12:25:54.407: E/AndroidRuntime(15158): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-19 12:25:54.407: E/AndroidRuntime(15158): at dalvik.system.NativeStart.main(Native Method)
08-19 12:25:54.407: E/AndroidRuntime(15158): Caused by: java.lang.NullPointerException
08-19 12:25:54.407: E/AndroidRuntime(15158): at com.webpage.WebActivity.onCreate(WebActivity.java:58)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.Activity.performCreate(Activity.java:5240)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
08-19 12:25:54.407: E/AndroidRuntime(15158): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2037)
08-19 12:25:54.407: E/AndroidRuntime(15158): ... 11 more
For the sake of a more complete answer:
The line that the NullPointerException occurred on is 58, which happens to be
mWebView.loadUrl("http://"(b.getString("iptextfield")) + "/index.html");
At first glance, mWebView, b.getString() or b could be null.
mWebView was already used before without issue so it shouldn't be null.
The String returned by b.getString() is concatenated with two other strings, so that won't throw an NPE.
So that leaves b itself.
This should make sense as in MainActivity, you are starting WebActivity but not adding any extras to its Intent.
Use Intent#putExtra() to add extras to the intent, before starting the next Activity.
Then, as #siik suggested, it is good practise to make sure your Bundle is not null:
if (b != null){
mWebView.loadUrl("http://" + (b.getString("iptextfield")) + "/index.html");
}
You could even go a little further and check to see if b actually contains the key you are looking for.
if (b != null && b.containsKey ("iptextfield")){
mWebView.loadUrl("http://" + (b.getString("iptextfield")) + "/index.html");
}
You need to check if Bundle b = getIntent().getExtras();
Returns any value, i.e.not null

Related Links

Error with duplicated FileProvider in manifest.xml with Cordova
Android - Socket.io - no logs, no connection
Get only username from textView and insert into database
how to start activity after call some requests by retrofit?
Releasing Mobile App and Testing App Store Features
How to set version-script file in CMakeLists.txt file of Android Studio
In android inapp subscription how do i maintain same userId across all devices for the single play store account
Android - Null Array. Runs in Android Studio emulator but not on real device when unplugged
Android Templates something like ASP.net Masterpages?
How to get data like as yuv data from Camera.PreviewCallback.onPreviewFrame(data, camera) from Surface?
Need Gradle build speed up (using 5 modules)
How to securely make login system in hybrid app (phonegap/cordova)
OpenCV on Android: minEnclosingCircle outputs null points
Change style of checked checkbox in Option menu item
SkobblerMaps Android: How to create navigation log file
Android, how to turn off audio powersave?

Categories

HOME
windows
eclipse
google-cloud-platform
nunit
apache-spark-mllib
serialization
limit
regular-language
zip
webix
nfs
pkcs#11
sendgrid-api-v3
little-proxy
mel
opengl-es-3.0
jsonpath
flatpak
android-intent
qsqlquery
capistrano3
alpacajs
symfony-2.8
atlassian-stash
fileserver
sencha-touch
php-mysqlidb
dynamically-generated
mount
quantlib
nsurl
quickfixj
instantiation
sendbird
installscript
pdw-file-browser
batching
docfx
imagenet
zoomify
openalpr
modernizr
filesystemwatcher
posixct
payara-micro
spring-data-couchbase
cmp
testrail
grails-plugin
xcode-server
envi
watch-os-2
rspec2
ng-grid
jtwig
query-by-example
turbo-prolog
nio2
blitline
highland.js
directdraw
toran-proxy
fuelcms
rubaxa-sortable
text-search
pax-exam
ember-cli-rails
haskell-warp
graphics2d
matlab-deployment
delayed-execution
gii
trdion2011
unix-socket
draw2d-js
paste
authlogic
file-not-found
koala
sbcl
video-codecs
mantle
joomla3.1
panda3d
qi4j
vfs
cuda-gdb
thttpd
predicatebuilder
node-redis
screen-size
workflow-services
exponentiation
mtu
office-2007
burndowncharts
django-piston
filesystemobject
todos
online-storage

Resources

Encrypt Message