我想在控制台打印一些东西,这样我就可以调试它。但出于某种原因,我的Android应用程序没有打印任何东西。

我怎么调试呢?

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");
    }

当前回答

我没有花哨的IDE使用LogCat,因为我使用一个移动IDE。

我必须使用各种其他方法,我有类和实用程序供您使用,如果您需要的话。

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.

由于Android控制台是一个单独的应用程序在一个单独的进程,如果你的应用程序崩溃,你肯定会看到错误。此外,IConsole在你的应用程序中设置了一个未捕获的异常处理程序,以防你不热衷于异常处理。它几乎打印堆栈跟踪和异常消息到Android控制台。最后,如果Android控制台崩溃,它会将堆栈跟踪和异常发送给你,你可以选择一个应用程序来读取它。实际上,AndroidConsole并不需要崩溃。

编辑额外的 我注意到我的APK Builder没有LogCat;助手。然后我意识到使用我的Android控制台的好处。

Android控制台被设计成只占据屏幕的一部分,所以你可以看到你的应用程序,以及应用程序发送到控制台的数据。这在AIDE中是不可能的。我想触摸屏幕,看到坐标,Android控制台让这很容易。 Android控制台的设计是当你写入时弹出。 Android控制台将隐藏当你反按。

其他回答

当然,要在logcat中看到结果,您应该至少将日志级别设置为“Info”(logcat中的日志级别);否则,就像我遇到的那样,您将看不到您的输出。

你的手机上没有地方可以读取System.out.println();

相反,如果你想查看某些结果,要么查看你的logcat/控制台窗口,要么让Toast或Snackbar(如果你在较新的设备上)出现在设备的屏幕上,并显示消息:) 这就是我所做的,当我必须检查例如它在开关情况代码!祝你编码愉快!:)

我将把这个留给更多的访问者,因为对我来说,这是关于主线程无法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;
}
}

用法:LogUtil。println("这是一个字符串");

System.out.println("…")会显示在Android Studio的Android Monitor上

是的。如果您正在使用模拟器,它将显示在系统下的Logcat视图中。标签。编写一些东西并在模拟器中进行尝试。