I've tested deployment to Android Market already as a test, seems like everything is hooked up fine - but now getting a little deeper into java and need to figure out how to use the Eclipse IDE.
I'm having exceptions, but where can see the exception message?.. it's not showing up in my consoles , not even the java stack trace console while in debug mode?
URLConnection uconn = null;
try {
uconn = u.openConnection();
catch (IOException e) {
e.printStackTrace();
x = x + e.toString();
}
Where does this output, not seeing it in any of the consoles. How can I output to console?
System.out.println("-----------------hello");
I can open up the view variable windows, but see no variables there, how do I add them into that so I can see their values, are they only they only visible if I add a line breakpoint? btw, what's the function key to step to next breakpoint?
Android exceptions are usually
visible at LogCat view, it is
available at DDMS perspective or you
can add it through menu Window ->
Show View -> Other.. -> Android
You'd better use log feature if you need to output something for debugging
Log.i("some tag", "message"); The log message is then visible at the LogCat view.
You need to stop at a breakpoint to see current value of variable, otherwise that makes not sense. Function keys from F5 till F8 are used to manipulate the flow. Usually F8 will bring you to the next breakpoint.
Related
I am developing plugin for eclipse. It will run my clang tool. In clang tool i am writing simple pass to check that every case and default statement must have the break statement.
I am running my clang tool using process builder.
I am using job API to run my tool and Display class to print the error.
when i run my plugin i am facing some issue.
Let say i commented my break statement of one of the case or default statement , it should print error in console that every case and default statement must have the break statement.
it is printing error after i type something else or some space or trying to uncomment after my comment .
similarly when i do uncomment , it should not print the error , but it is printing , but again when i type something or some space or trying to comment , error disappear.
following is the link of my code
https://github.com/sunilsarode/eclipse_plugin/blob/master/ccchecker/src/ccchecker/handlers/SampleHandler.java
following image show the first case not printing error on my eclipse console on which i am developing a plugin .
and this shows that second case printing error on my eclipse console on which i am developing a plugin .
any help in this ?
EDIT:
I want my plugin-in to output something on console view (not on development console ) or some dot kind of thing on the gutter of the text editor and when i click or hover on that dot , i want to show some popup with error message.
I tried to print message on console view by crating console using this link https://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F
it works , but new problem is , i lost focus from text editor to console that i have created and because of this i am unable to test the problem i mentioned in question.
sorry too much i am expecting.
Following image show that text editor lost its focus to console view and the button you have mentioned is not there at console view but it is there at console of eclipse on which i am writing a code.
Following keypress method and addKeyListener is not working when i press the Ctrl+s. In order to get the save event we have to use the ICommandService with save command.
((StyledText) editor.getAdapter(Control.class)).addKeyListener(new KeyListener() {
#Override
public void keyPressed(KeyEvent keyEvent) {
if (((keyEvent.stateMask & SWT.CTRL) == SWT.CTRL) && (keyEvent.keyCode == 's')) {
// System.out.println("From Display I am the Key down !!" + keyEvent.keyCode);
Job job = new Job("My job") {
#Override
protected IStatus run(IProgressMonitor arg0) {
// TODO Auto-generated method stub
StringBuilder builder = runCCChecker(path);
syncWithUi(builder);
return Status.OK_STATUS;
}
};
job.setUser(true);
job.schedule();
}
}
#Override
public void keyReleased(KeyEvent arg0) {
}
});
In order to show the warning at each line I am using IMarker interface.
In order to run my tool only on current active file of eclipse editor on which I am firing save command , I am using IPartService service.
following is the link of my code
https://github.com/sunilsarode/eclipse_plugin/blob/master/ccchecker/src/ccchecker/handlers/SampleHandler.java
It is working fine. If anybody find any issue please let me know.
I have two smartphones one Nexus 4 and one Vodafone Smart 3 (TLC - Alcatel v975). In my Sourcecode I have two log.d and if I make the debugging on Nexus the log.d appear in Logcat, but when I'm using Vodafone Smart 3 , log.d doesn't appear in Logcat ... My Sourcecode is the following :
public void savetime(View view){
Integer hour= mitsos.getCurrentHour();
Integer minutes=mitsos.getCurrentMinute();
Log.d("mitsos","hour "+hour);
Log.d("mitsos","minutes"+minutes);}
Some device not print debug log, but you can use below method
Log.wtf("tag","msg");
its working for me.
Try "Unable to open log device '/dev/log/main': No such file or directory", set the log level.
If you don't see the service menu, make sure that your ROM support it, otherwise you maybe need to use another ROM (usually official) or just use Log.i() instead.
you can use System.out.println("") as well for wrting to log cats.I have also come across with the same problem on my device.Sometime device did log but sometime didnt. but System.out.println("") always works for me.
I have an web application, which generates a JS message
'Are you sure you want to navigate away from this page?' when you try to open a new page.
I know I can handle this message by
Selenium.getAlert();
(or some modification of it, I haven't tried it yet)
But my main problem is that this message generates only when I leave this page.
In selenium I can leave page by using
Selenium.open("new address");
or
Selenium.back();
So I use code like this
Selenium.open("new address");
Selenium.getAlert();
But the problem is that Selenium.open doesn't finish and go to next code line in program until the new page is fully loaded, but the page can't be loaded until program goes to next code line and handles this alert. So it's ablocked situation and I don't know how to handle it.
I don't think it can be done, staying entirely within the Selenium RC API. I have cases similar to this that I handle by launching an AutoIt script, before the open(), that waits for the prompt and answers it. That only works on Windows, but if you need something for other systems, I'm sure there are equivalent tools.
Selenium has always had a problem with alerts and confirmations (which this is - a confirmation has an OK/Cancel choice) that occur duing page loading. There's even an ancient bug number enshrined in one of the error messages that explains that it can't catch them.
Use:
openAndWait(..)
Maybe this other SO question will help you.
I don't know if this will work, but it's an idea. You could try something like:
try {
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
Selenium.open("new address");
} catch (Exception e) {
// Should throw after 1 second
}
// Now we may be able to interact with the alert.
Selenium.getAlert();
Is there any easy to follow tutorial for debugging Java / J2EE applications in eclipse?
A step by step guide on how to check for unchecked and checked exceptions?
I have been trying to find on the internet, but to no use.
To add a Java Exception Breakpoint: Select the "Breakpoints" view in the Debug perspective and click on the view toolbar button for exception breakpoints (or choose Run->Add Java Exception Brekakpoint). In the next dialog, type the name of the exception (camel case is support, simply type NPE if you want to catch NullPointerExceptions, you'll get a list matching items), select it from the list and press OK. C'est ca.
You can activate/deactivate this special breakpoint from the Breakpoints view like normal breakpoints.
More fun: right click on a breakpoint entry in the Breakpoints view and choose "Breakpoint Properties". There you can add extra conditions, like 'only break when myCustomString.equals("WTF")' or something else. But conditional breakpoints will slow down the application significantly, only activate them if you really need them.
The debugger in Eclipse will automatically suspend execution (and let you inspect variables/step/resume etc) if an uncaught exception is thrown.
Enter for instance the following program...
public class Test {
public void methodA() {
methodB("hello");
methodB(null);
}
public void methodB(String s) {
System.out.println(s.substring(2));
}
public static void main(String[] args) {
new Test().methodA();
}
}
... and click the little "bug"-icon or choose Run -> Debug As -> Java Application
A few useful tutorials
Debugging a Java Program with Eclipse
Java Debugging with Eclipse - Tutorial
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.println gets redirected to LogCat and printed using Log.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.println messages get lost. In the same way this happens when you run a "traditional" Java application with javaw.
Instead, you can use the Android Log class:
Log.d("MyApp","I am here");
You can then view the log either in the Logcat view in Eclipse, or by running the following command:
adb logcat
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 String somewhere.
Log.d(MyActivity.LOG_TAG,"Application started");
There are five one-letter methods in Log corresponding to the following levels:
e() - Error
w() - Warning
i() - Information
d() - Debug
v() - Verbose
wtf() - What a Terrible Failure
The documentation says the following about the levels:
Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and info logs are always kept.
Use the Log class. Output visible with LogCat
Yes it does. If you're using the emulator, it will show in the Logcat view under the System.out tag. Write something and try it in your emulator.
Of course, to see the result in logcat, you should set the Log level at least to "Info" (Log level in logcat); otherwise, as it happened to me, you won't see your output.
if you really need System.out.println to work(eg. it's called from third party library). you can simply use reflection to change out field in System.class:
try{
Field outField = System.class.getDeclaredField("out");
Field modifiersField = Field.class.getDeclaredField("accessFlags");
modifiersField.setAccessible(true);
modifiersField.set(outField, outField.getModifiers() & ~Modifier.FINAL);
outField.setAccessible(true);
outField.set(null, new PrintStream(new RedirectLogOutputStream());
}catch(NoSuchFieldException e){
e.printStackTrace();
}catch(IllegalAccessException e){
e.printStackTrace();
}
RedirectLogOutputStream class:
public class RedirectLogOutputStream extends OutputStream{
private String mCache;
#Override
public void write(int b) throws IOException{
if(mCache == null) mCache = "";
if(((char) b) == '\n'){
Log.i("redirect from system.out", mCache);
mCache = "";
}else{
mCache += (char) b;
}
}
}
it is not displayed in your application... it is under your emulator's logcat
System.out.println("...") is displayed on the Android Monitor in Android Studio
There is no place on your phone that you can read the System.out.println();
Instead, if you want to see the result of something either look at your logcat/console window or make a Toast or a Snackbar (if you're on a newer device) appear on the device's screen with the message :)
That's what i do when i have to check for example where it goes in a switch case code! Have fun coding! :)
I'll leave this for further visitors as for me it was something about the main thread being unable to System.out.println.
public class LogUtil {
private static String log = "";
private static boolean started = false;
public static void print(String s) {
//Start the thread unless it's already running
if(!started) {
start();
}
//Append a String to the log
log += s;
}
public static void println(String s) {
//Start the thread unless it's already running
if(!started) {
start();
}
//Append a String to the log with a newline.
//NOTE: Change to print(s + "\n") if you don't want it to trim the last newline.
log += (s.endsWith("\n") )? s : (s + "\n");
}
private static void start() {
//Creates a new Thread responsible for showing the logs.
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
while(true) {
//Execute 100 times per second to save CPU cycles.
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
//If the log variable has any contents...
if(!log.isEmpty()) {
//...print it and clear the log variable for new data.
System.out.print(log);
log = "";
}
}
}
});
thread.start();
started = true;
}
}
Usage: LogUtil.println("This is a string");
I dont having fancy IDE to use LogCat as I use a mobile IDE.
I had to use various other methods and I have the classes and utilties for you to use if you need.
class jav.android.Msg. Has a collection of static methods.
A: methods for printing android TOASTS.
B: methods for popping up a dialog box.
Each method requires a valid Context. You can set the default context.
A more ambitious way, An Android Console. You instantiate a handle to the console in your app, which fires up the console(if it is installed), and you can write to the console. I recently updated the console to implement reading input from the console. Which doesnt return until the input is recieved, like a regular console.
A: Download and install Android Console( get it from me)
B: A java file is shipped with it(jav.android.console.IConsole). Place it at the appropriate directory. It contains the methods to operate Android Console.
C: Call the constructor which completes the initialization.
D: read<*> and write the console.
There is still work to do. Namely, since OnServiceConnected is not called immediately, You cannot use IConsole in the same function you instantiated it.
Before creating Android Console, I created Console Dialog, which was a dialog operating in the same app to resemble a console. Pro: no need to wait on OnServiceConnected to use it. Con: When app crashes, you dont get the message that crashed the app.
Since Android Console is a seperate app in a seperate process, if your app crashes, you definately get to see the error. Furthermore IConsole sets an uncaught exception handler in your app incase you are not keen in exception handling. It pretty much prints the stack traces and exception messages to Android Console. Finally, if Android Console crashes, it sends its stacktrace and exceptions to you and you can choose an app to read it. Actually, AndroidConsole is not required to crash.
Edit Extras
I noticed that my while APK Builder has no LogCat; AIDE does. Then I realized a pro of using my Android Console anyhow.
Android Console is design to take up only a portion of the screen, so you can see both your app, and data emitted from your app to the console. This is not possible with AIDE. So I I want to touch the screen and see coordinates, Android Console makes this easy.
Android Console is designed to pop up when you write to it.
Android Console will hide when you backpress.
Solution that worked for me:
Under Logcat. (To show Logcat if not already shown. Click View menu-->Tool Windows-->Logcat). It is shown as System.out not as System.out.println as you might expect it. Rebuild the app if you have not already.
In the picture, highlighted yellow shows the System.out and output "Hello again".
Recently I noticed the same issue in Android Studio 3.3. I closed the other Android studio projects and Logcat started working. The accepted answer above is not logical at all.