简体   繁体   English

我在 Android Studio logcat 中看不到我的日志

[英]I can't see my logs in Android Studio logcat

Here's a simple app, I'm trying to create logs in the printToLogs method.这是一个简单的应用程序,我正在尝试在 printToLogs 方法中创建日志。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.v("Log0","test");
}

public void printToLogs(View view){
    TextView menuTextView1 = findViewById(R.id.menu_item_1);
    TextView menuTextView2 = findViewById(R.id.menu_item_2);
    TextView menuTextView3 = findViewById(R.id.menu_item_3);

    String item1 = (String) menuTextView1.getText();
    String item2 = (String) menuTextView2.getText();
    String item3 = (String) menuTextView3.getText();

    Log.v("Log1",item1);
    Log.v("Log2",item2);
    Log.v("Log3",item3);

}

but logs with the tags Log1, Log2, Log3 are not shown at all in the logcat, what does show up is the Log0 in the onCreate method, but the other ones in printToLogs never show up when I search for them at all .但随着标签LOG1,LOG2,LOG3日志不会在所有在logcat中所示,这是什么显示的是Log0在onCreate方法,但在printToLogs其他的人从来没有出现在我的所有搜索。 I attempted re-installing the app and restarting logging.我尝试重新安装应用程序并重新启动日志记录。 That didn't work.那没有用。

The menu items are: Mango sorbet, Blueberry pie, Chocolate lava cake.菜单项目有:芒果冰糕、蓝莓派、巧克力熔岩蛋糕。 And yes, I tried searching for them, and they are not in the logcat either.是的,我尝试搜索它们,但它们也不在 logcat 中。

If this is your actual code, you aren't even calling printToLogs in the onCreate method.如果这是您的实际代码,您甚至printToLogsonCreate方法中调用printToLogs You should be more diligent before posting something simple like this.在发布这样简单的内容之前,您应该更加勤奋。

Barring a serious runtime environment issue, this problem should be fairly easy to solve.除非出现严重的运行时环境问题,否则这个问题应该很容易解决。

It seems as if the printToLogs(View view) method is to be executed in response to the user clicking a button.似乎要响应用户单击按钮来执行 printToLogs(View view) 方法。 If so, try including the following line in your activity_main.xml if you haven't already:如果是这样,请尝试在您的 activity_main.xml 中包含以下行(如果您还没有):

android:onClick="printToLogs"

This will bind the button on the UI with the printToLogs(View view) method.这将使用 printToLogs(View view) 方法绑定 UI 上的按钮。

If, on the other hand, printToLogs(View view) is intended to be a standalone method (ie one that should execute regardless of user input) it should not accept a View as an argument.另一方面,如果 printToLogs(View view) 旨在成为一个独立的方法(即无论用户输入如何都应执行的方法),则不应接受 View 作为参数。 For your purposes, its parameter list should be completely empty.出于您的目的,它的参数列表应该完全为空。 In other words, the method signature should read:换句话说,方法签名应为:

public void printToLogs()

Also, it should be called within the onCreate(Bundle savedInstances) method.此外,它应该在 onCreate(Bundle savedInstances) 方法中调用。 Add the following to the onCreate(Bundle savedInstances) method:将以下内容添加到 onCreate(Bundle savedInstances) 方法:

printToLogs();

This will initiate the execution of the method as soon as the app begins to run.一旦应用程序开始运行,这将启动方法的执行。

Make sure the logcat filter is set to "Verbose" when testing like so: (img is link since apparently I need 10 rep. to embed images into answers directly) logcat filter确保在测试时将 logcat 过滤器设置为“详细”:(img 是链接,因为显然我需要 10 个代表才能将图像直接嵌入到答案中) logcat 过滤器

嘿,通过使用属性部分或仅 android:onClick 在您的 xml 文件中将您的方法/函数名称添加到您的按钮中,然后它将被解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM