我想在控制台打印一些东西,这样我就可以调试它。但出于某种原因,我的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");
    }

当前回答

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

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

其他回答

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

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

如果你真的需要System.out.println工作(例如。它是从第三方库调用的)。你可以简单地使用反射来更改System.class中的out字段:

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类:

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

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

对我有效的解决方案:

在Logcat中。(如果尚未显示Logcat,则显示Logcat。点击查看菜单——>工具窗口——>Logcat)。它显示为System。不是像你期望的那样System.out.println。如果你还没有重新构建应用程序。

在图片中,黄色突出显示的是系统。输出“Hello again”。