I want to print something in console, so that I can debug it. But for some reason, nothing prints in my Android application.
How do I debug then?
public class HelloWebview extends Activity {
WebView webview;
private static final String LOG_TAG = "WebViewDemo";
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new MyWebChromeClient());
webview.loadUrl("http://example.com/");
System.out.println("I am here");
}
Correction:
On the emulator and most devices
System.out.printlngets redirected to LogCat and printed usingLog.i(). This may not be true on very old or custom Android versions.Original:
There is no console to send the messages to so the
System.out.printlnmessages get lost. In the same way this happens when you run a “traditional” Java application withjavaw.Instead, you can use the Android
Logclass:You can then view the log either in the Logcat view in Eclipse, or by running the following command:
It’s good to get in to the habit of looking at logcat output as that is also where the Stack Traces of any uncaught Exceptions are displayed.
The first Entry to every logging call is the log tag which identifies the source of the log message. This is helpful as you can filter the output of the log to show just your messages. To make sure that you’re consistent with your log tag it’s probably best to define it once as a
static final Stringsomewhere.There are five one-letter methods in
Logcorresponding to the following levels:e()– Errorw()– Warningi()– Informationd()– Debugv()– Verbosewtf()– What a Terrible FailureThe documentation says the following about the levels: