The question is published on by Tutorial Guruji team.
I’m a noob in programming and I’m doing a WebView from a tracking website. The problem is, when I type the tracking code in the WebView the site doesn’t show anything, I will show to you in next images:
Here is my code:
public class MainActivity extends AppCompatActivity { private WebView webview; private static final String TAG = "Main"; private ProgressDialog progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.webview = (WebView)findViewById(R.id.webview); WebSettings settings = webview.getSettings(); settings.setJavaScriptEnabled(true); webview.addJavascriptInterface(new WebAppInterface(this), "android"); webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); progressBar = ProgressDialog.show(MainActivity.this, "WebView Example", "Loading..."); webview.setWebViewClient(new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view, String url){ Log.i(TAG, "Processing webview url click..."); view.loadUrl(url); return true; } public void onPageFinished(WebView view, String url){ Log.i(TAG, "Finished loading url: " +url); if (progressBar.isShowing()){ progressBar.dismiss(); } } public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){ Log.e(TAG, "Error: " + description); Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show(); alertDialog.setTitle("Error"); alertDialog.setMessage(description); alertDialog.show(); } }); webview.loadUrl("https://geartrack.hdn.pt"); } public class WebAppInterface{ Context mContext; WebAppInterface(Context c){ mContext = c; } @JavascriptInterface public void showToast(String toast){ Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater().inflate(R.menu.mainmenu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item){ switch (item.getItemId()){ case R.id.action_refresh: webview.loadUrl("https://geartrack.hdn.pt"); return true; } return super.onOptionsItemSelected(item); } @Override public boolean onKeyDown(int keyCode, KeyEvent event){ if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()){ webview.goBack(); return true; } return super.onKeyDown(keyCode, event); } }
Please help me.
Answer
The problem is you haven’t enabled LocalStorage for your WebView. If you use Chrome to inspect your WebView via developer tools, you will notice that you have an error. Specifically main.js?v=1:373 Uncaught TypeError: Cannot read property 'length' of null
Why is this? If you look at that line of code in main.js
you will see that the script is accessing length of localStorage
. It will crash the script because you can not access a property of an undefined variable. localStorage
is undefined in a WebView by default, because DomStorage
is not enabled.
To fix this. Enable LocalStorage for your WebView add the line:
settings.setDomStorageEnabled(true);
Where settings
is the WebView
‘s WebSettings
object.
After doing this everything works as you should expect!