Is it possible to see which functions are called in Logcat? - android

I know, there is a way to see which functions are called in log-cat is to write a log message on top for every function like this
#Override
protected void onDestroy() {
super.onDestroy();
Log.d("myTag","onDestroy function is called!");
// some logic
}
But it becomes irritating when you have more function.
So, I wonder if there is a way to see which functions are called in adb-logcat without writing log messages for every function.
I hope they can be fetched from somewhere in the stack but I couldn't find it.

You can try Hugo. In that case you have to annotate your methods with #DebugLog only. Then Hugo will generate logs for you (and will print out arguments and return value!). Example from GitHub:
#DebugLog
public String getName(String first, String last) {
SystemClock.sleep(15); // Don't ever really do this!
return first + " " + last;
}
And log output:
V/Example: ⇢ getName(first="Jake", last="Wharton")
V/Example: ⇠ getName [16ms] = "Jake Wharton"

Instead of printing log in every function. I (or most of the people) would suggest you to put debug.
To use debug first create breakpoints inside every function you want to check. To apply breakpoints simply left click in the area to the left of your code (refer image the pink circle represents a break-point).
Then to use Debug you have to press this button after successfully running your application.
As soon as the first method is called your application will pause at the break-point, then you can use F8 (or F6 if you are using eclipse settings) to move to next line, to move to next break-point you can press F9(or F8 if you are using eclipse settings). this way to can check all the functions being called.
This break-point method is really helpful if you just want to make sure that a particular function is being called.
Other than this if you still insist to know the details of all the functions you can store the stacktrace.
final StackTraceElement[] trace = new Throwable().getStackTrace())
StackTraceElement STrace = trace[1];
String className = STrace.getMethodName();

Related

How to pass String from onClick Edit Text field to Background task - Android

How can i take user input from popup dialogue and pass it into a BackGround Async Task?
I have a "createGarden" button. When i click it i want to retrieve a string from user, and pass it to my Background AsyncTask.
In my onClick, i have tried calling String myGardenName = getGardenName(), which returns input from dialogue. Then passing this into my Background task.
new HomeBackgroundTask().execute("create_garden", UserID_String,myGardenName );
I also tried using a value container, and passing this instead of "myGardenName":
String myGN = gnVC.getVal();
HomeBackgroundTask mhomebackgroundtask1 = new HomeBackgroundTask();
mhomebackgroundtask1.execute("create_garden", UserID_String, myGN);
instantiated my 'gardenValueContainer' value container as final in my "getGardenName()" method (enclosing class?) as well as instantiating it in my onCreate()
- I then try SETTING that value from within my onClick (inner class?)
--Also tried calling my HomeBackgroundTask directly from the onClick
Problem
Seems that my create garden always tries to insert a BLANK as the garden name. resulting in "garden "" already exists". When debugging, the user input get's passed through as a paramater, there was an issue with moving from "onPreExectute" to "doInBackground" but now when i'm debugging i get stuck in looper where i can't step over/into/out and my app just says freezes on connecting. (debug halts on a comment line, which might be bad?)
My php scripts work just fine with the same logic for registering a user.
No errors in my console!
http://pastebin.com/2AzWmcM5
Any help greatly appreciated!
"debug halts on a comment line, which might be bad"
Have you tried removing all breakpoints, or putting breakpoints on method implementations rather than on the, say, first line of the method?

Robotium Solo - wait for broadcast

I want to create a condition to wait for a broadcast upon a button press
right now I am just doing solo.sleep(10000)
but I dont want to sleep solo for nothing
How do I formulate the condition "broadcast received" ?
Ok explanations
Robotium Solo is an instrumentation framework with nice api
It has a method called "solo.waitForCondition(Condition, int timeout)"
I want to formulate (the word formulate means say what i want to say in correct words)
the correct condition that will tell me that the broadcast was indeed received
I want to write some code (I don't know which exactly) to know that the broadcast was indeed sent
for example, if i want to know that a button is now visible i would write
solo.waitForCondition(new Condition(){
public boolean isSatisfied(){
Button b = getActivity().findViewById(R.id.myButton);
return b.getVisibility() == View.VISIBLE;
}
}
now back to my question - What (not how, but what) do I write in order to know for sure that the broadcast was sent inside the isSatisfied method
I suppose you meant that you don't want to sleep for 10 seconds, if you get the broadcast earlier. What you can do is
long beginTime = new Date().getTime();
while (new Date().getTime() - beginTime < 10000) {
solo.sleep(500);
if (conditionMet) {
// Do something
break;
}
}
This way you can do these checks on smaller intervals.
Ok, so in fact this is more or less how waitForCondition is implemented. Unfortunately I don't think you can listen for events with robotium. What you can do is monitor the view hierarchy. In your case, there should be some difference to the views that is triggered when the button is clicked, so that is what you need to check for in the Condition (and your example does that).
This is if you don't want to edit the code you are testing. If you are willing to change the code, you can add an onClickListener() and in that you can set a view's Tag to a boolean for example. Later in robotium you can check for that tag for being set. This is however not good way to do it, because you are adding more code just for the sake of the tests.

For loop not terminating - Android

I don't know why but my for loop won't 'stop' when its reached the truth of the termination statement.
for(int i = 1; i < 11; i++){
edittext.setText("");
EasyGame();
//if(i==10){
//Game.this.finish();
//}
}
EasyGame() is an arithmetic method, just adds two numbers together. I tried using the if statement shown above, but it still wouldn't do anything, and if it did it would call finish() after the first question!
If someone would be kind to help me I would be grateful.
EDIT:
public void EasyGame(){
Random rand = new Random();
final int a = (int) rand.nextInt(20)+1;
final int b = (int) rand.nextInt(20)+1;
String aString = Integer.toString(a);
String bString = Integer.toString(b);
String display = aString + " + " + bString + " =";
questionLabel.setText(display);
c = a + b;
}
that for loop is inside a switch/case, which deals with onClick() for buttons
Very difficult to say without seeing more code, but what you have posted alone is inherently flawed because you're trying to continuously update a UI element within a loop. If that loop is running on the UI thread, then the system isn't going to be able to redraw any UI elements such as your edittext until the loop (and whatever containing callback method) exits.
Therefore, when you say "I tried using the if statement shown above, but it still wouldn't do anything, and if it did it would call finish() after the first question!" I make the assumption that you're believing that the loop is only iterating once because you only ever see edittext display whatever is passed in the last ever .setText() call.
I dont see anything wrong with that code. The only possible problem is that your counter is being decremented somewhere (for example inside EasyGame();), or the problem is somewhere else
In the lack of provided code, I assume that your indefinite loop is happening inside EasyGame() method
The loop probably only runs once, and then it gets halted by a never ending loop in EasyGame().
Note: Don't use initial capitalized letters for methods, it's confusing.
I agree with Trevor Page, and i'll try and clarify :
your code, if it runs on a callback on the UI thread, could be calling itself by generating a callback when you are clearing the first textView or modifying the second.
Also, what do you mean by 'it won't stop' ? I don't quite see what you are trying to do here, since you erase 10 times the content of a textView and replace 10 times the content of another one.

Measure time for rendering ListView

How can I measure time that ListView/ListActivity takes for rendering its list?
I have a ListActivity that takes a long time to show (on older devices it's ~3s) and I'd like to optimize that.
EDIT:
I already have time between Activity1 and Activity2. During this time, Activity2 is doing some stuff (initializing). Among other things, it renders its list. I want to get time this activity takes to render that list. If total time between activities is 3s, I want to know whether rendering list takes 2.9s or 0.5s....
You could simply ouput the time. For example you could use the logcat
final long t0 = System.currentTimeMillis();
// code to measure
Log.w(TAG, "TEXT" + System.currentTimeMillis()-t0);
Of course you could use any other system for the ouput like a dialog or stuff. Just use what you like.
EDIT:
If you don't want to use a debug message in your code all the time you could do it like this:
Create a class called settings:
public class Settings {
public static final boolean DEBUG = true;
// If you prefer you could do use an enum
// enum debugLevel {SHOW_EVERYMESSAGE, ERRORS, IMPORTANT_MESSAGES, ...}
// In your classes you would have to check that DEBUG is less or equal than
// the debugLevel you want
}
In classes where you want to use a debug message simply do this
import xxx.yyy.Settings
class foo {
final static boolean DEBUG = Settings.DEBUG;
if(DEBUG){
// Debug messages
}
}
Now if you want to disable DEBUG messages you could simply set DEBUG = false in your Settings class.
If you want to measure between two activities you could use intents and send t0 with an intent to the other activity to compute the time. Of course you could include this with if(DEBUG){ /* code */ } statements to spare the sending of the intent in the final release. The if statements should not increase the computation of your code too dramatically.
I cannot tell if Java offers a better implementation using System.currentTimeMillis() or System.nanoTime(). Nevertheless, you should give the TimingLogger class a try. Take a look at this article describing the usage of the TimingLogger helper class.

Android: How can I print a variable on eclipse console?

I wanted to print the value of a variable on the console for my debugging purpose, but System.out.println doesn't work.
System.out.println and Log.d both go to LogCat, not the Console.
Window->Show View->Other…->Android->LogCat
I'm new to Android development and I do this:
1) Create a class:
import android.util.Log;
public final class Debug{
private Debug (){}
public static void out (Object msg){
Log.i ("info", msg.toString ());
}
}
When you finish the project delete the class.
2) To print a message to the LogCat write:
Debug.out ("something");
3) Create a filter in the LogCat and write "info" in the input "by Log Tag". All your messages will be written here. :)
Tip: Create another filter to filter all errors to debug easily.
Writing the followin code to print anything on LogCat works perfectly fine!!
int score=0;
score++;
System.out.println(score);
prints score on LogCat.Try this
I think the toast maybe a good method to show the value of a variable!
Ok, Toast is no complex but it need a context object to work, it could be MyActivity.this, then you can write:
Toast.maketext(MyActivity.this, "Toast text to show", Toast.LENGTH_SHORT).show();
Although Toast is a UI resource, then using it in another thread different to ui thread, will send an error or simply not work
If you want to print a variable, put the variable name.toString() and concat that with text you want in the maketext String parameter ;)
toast is a bad idea, it's far too "complex" to print the value of a variable. use log or s.o.p, and as drawnonward already said, their output goes to logcat. it only makes sense if you want to expose this information to the end-user...
If the code you're testing is relatively simple then you can just create a regular Java project in the Package Explorer and copy the code across, run it and fix it there, then copy it back into your Android project.
The fact that System.out is redirected is pretty annoying for quickly testing simple methods, but that's the easiest solution I've found, rather than having to run the device emulator just to see if a regular expression works.
By the way, in case you dont know what is the exact location of your JSONObject inside your JSONArray i suggest using the following code: (I assumed that "jsonArray" is your main variable with all the data, and i'm searching the exact object inside the array with equals function)
JSONArray list = new JSONArray();
if (jsonArray != null){
int len = jsonArray.length();
for (int i=0;i<len;i++)
{
boolean flag;
try {
flag = jsonArray.get(i).toString().equals(obj.toString());
//Excluding the item at position
if (!flag)
{
list.put(jsonArray.get(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
jsonArray = list;

Categories

Resources