cannot resolve symbol 'tag'

Viewed 1230

I am beginner in learning android java. I learning the java android tutorial from Youtube and I follow exactly the coding from the video, but my coding showing "cannot resolve symbol 'tag' " on the android studio. May I know what is the problem? and hope to get explanation from all the master here.

package com.NewApplicationLifeCycle;

import ...

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i( tag: "State", msg: "onCreate" );
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i( tag: "State", msg: "onStart" );
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i( tag: "State", msg: "onResume" );
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i( tag: "State", msg: "onPause" );
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i( tag: "State", msg: "onStop" );
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i( tag: "State", msg: "onRestart" );
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i( tag: "State", msg: "onDestroy" );
    }
}
3 Answers

IDEs give you more context about the parameters of the function you're calling by highlighting the parameter names. Like in this case Log.i(tag: String, msg: String) accepts two parameters tag and msg but you don't have to write them yourself when calling the function.

Remove tag: and msg: from Log.i(...) calls and call it like this:

Log.i( "State", "onRestart" );

and so on for others as well.

You are not calling the log.i method correctly. This is how it is defined in the platform

 /**
 * Send an {@link #INFO} log message.
 * @param tag Used to identify the source of a log message.  It usually identifies
 *        the class or activity where the log call occurs.
 * @param msg The message you would like logged.
 */
public static int i(String tag, String msg) {
    return println(LOG_ID_MAIN, INFO, tag, msg);
}

meaning you need to pass 2 string values when calling the method. correct way to use it will be this way

 Log.i( "State","onStart" )

the tag and message will be shown automatically by android studio you do not need to put them since they are just an IDE feature to make the code readable

use like this

var tag = "your tag name"
var msg = "you message"

Log.i( tag +"State", msg+ "onCreate" );

otherwise :-

Log.d(tag, message)

Log.i( "your tag name","your message" );
  • and check this official link
    how can i Write and View Logs with Logcat

Related