简体   繁体   中英

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.

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 . 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.

If this is your actual code, you aren't even calling printToLogs in the onCreate method. 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. If so, try including the following line in your activity_main.xml if you haven't already:

android:onClick="printToLogs"

This will bind the button on the UI with the printToLogs(View view) method.

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. 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. Add the following to the onCreate(Bundle savedInstances) method:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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