Android:如何在 Eclipse 控制台上打印变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2752472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How can i print a variable on eclipse console
提问by SuperCop
I wanted to print the value of a variable on the console for my debugging purpose but system.out.println doesnt work.
我想出于调试目的在控制台上打印变量的值,但 system.out.println 不起作用。
回答by drawnonward
System.out.println
and Log.d
both go to LogCat, not the Console.
System.out.println
并且Log.d
都转到 LogCat,而不是控制台。
回答by doemsche
Window->Show View->Other…->Android->LogCat
窗口->显示视图->其他…->Android->LogCat
回答by poojan9118
Writing the followin code to print anything on LogCat works perfectly fine!!
编写以下代码以在 LogCat 上打印任何内容都可以正常工作!!
int score=0;
score++;
System.out.println(score);
prints score on LogCat.Try this
在 LogCat 上打印分数。试试这个
回答by Gabriel Llamas
I'm new to Android development and I do this:
我是 Android 开发的新手,我这样做:
1) Create a class:
1)创建一个类:
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:
2) 打印一条消息到 LogCat 写:
Debug.out ("something");
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. :)
3) 在 LogCat 中创建过滤器,并在输入“by Log Tag”中写入“info”。您的所有信息都会写在这里。:)
Tip: Create another filter to filter all errors to debug easily.
提示:创建另一个过滤器来过滤所有错误以便轻松调试。
回答by daisy
I think the toast maybe a good method to show the value of a variable!
我认为 toast 可能是显示变量值的好方法!
回答by Sebasu
Ok, Toast is no complex but it need a context object to work, it could be MyActivity.this
, then you can write:
好的,Toast 并不复杂,但它需要一个上下文对象才能工作,它可以是MyActivity.this
,然后你可以写:
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 是一个 UI 资源,那么在另一个不同于 ui 线程的线程中使用它,会发送错误或根本不起作用如果要打印变量,请将变量name.toString()
和您想要的文本连接到 maketext 字符串参数中; )
回答by Avi Levin
By the way, in case you dont know what is the exact locationof 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)
顺便说一句,如果您不知道JSONObject 在 JSONArray 中的确切位置,我建议使用以下代码:(我假设“jsonArray”是您包含所有数据的主要变量,我正在搜索确切的具有equals函数的数组内的对象)
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;
回答by Haravikk
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.
如果您正在测试的代码相对简单,那么您只需在包资源管理器中创建一个常规 Java 项目并复制代码,运行它并在那里修复它,然后将其复制回您的 Android 项目。
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.
System.out 被重定向的事实对于快速测试简单方法来说非常烦人,但这是我找到的最简单的解决方案,而不必运行设备模拟器只是为了查看正则表达式是否有效。
回答by MJB
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...
toast 是个坏主意,打印变量的值太“复杂”了。使用 log 或 sop,正如drawonward 已经说过的那样,它们的输出会转到 logcat。只有当您想将此信息公开给最终用户时才有意义...