Call function from another class - there is an error: cannot find symbol

Hi,
I’ve got a “error: cannot find symbol” problem.

Example:
Page 1:
@JvmStatic // maybe, I don’t know
fun text(text: String?, show: Boolean?, result: Boolean) {
if (result) {
viewBinding?.buttonRun?.setVisibility(View.GONE)
viewBinding?.buttonDone?.setVisibility(View.VISIBLE)
viewBinding?.uploadDoneText?.setVisibility(View.VISIBLE)
} else {
viewBinding?.buttonRun?.setText(text)
if (show != null) {
viewBinding?.buttonRun?.setEnabled(show)
}
}
}

Page 2:
Page1.text(uploadContext.getString(R.string.Upload_Wait), false,false);

It ends with this error: cannot find symbol
Page1.text(someContext.getString(R.string.Some_Text), false,false);
^
symbol: variable Page1
location: class Page2

Why cannot find symbol? Some problem with Page1.text?

Top-level functions are not part of any class (at least from the Kotlin perspective), we call them directly: text().

1 Like

Thank you! It looks good but there’s a new error: cannot find symbol
import static something.text;
^
Where is the problem right now?

Please provide full contents of your files and their locations in the source tree.

1 Like

Yes! I’m only added some things into OpenTracks. So I’ve got class UploadAsync where I’ve got this things (for example in onPreExecute or in onPostExecute):
public class UploadAsync extends AsyncTask<Void, Void, String> {

UploadActivity.text(uploadContext.getString(R.string.Upload_Wait), false,false);
UploadActivity.toast(uploadContext, uploadContext.getString(R.string.Upload_Wait));

}

And:
class UploadActivity : AbstractActivity() {

@JvmStatic
fun text(text: String?, show: Boolean?, result: Boolean) {
if (result) {
viewBinding?.buttonRun?.setVisibility(View.GONE)
viewBinding?.buttonDone?.setVisibility(View.VISIBLE)
viewBinding?.uploadDoneText?.setVisibility(View.VISIBLE)
} else {
viewBinding?.buttonRun?.setText(text)
if (show != null) {
viewBinding?.buttonRun?.setEnabled(show)
}
}
}

}
Anything else of content is unrelated. I think. I need to call function “text” from other classes.

If text() is defined inside the UploadActivity class, then it is a member function and it requires an instance of UploadActivity in order to be invoked. Kotlin doesn’t support static members. Please read about companion objects.

1 Like

I understand. Yes - text() is defined in UploadActivity. Or where can I defined it?

Android studio discovered for me this code:
import static …UploadActivity.text;

It looks good but build APKs again ends with this error: error: cannot find symbol
import static …UploadActivity.text;

That’s some little thing - I know. But I don’t know which.

As I said, it is not a static function and it can’t be invoked like this: UploadActivity.text(). You need an instance of UploadActivity to invoke it. If you wanted text() to be a static function, then again, Kotlin doesn’t support this. Create a top-level function or use a companion object.

1 Like

I understand. How to create top-level function?

I tried again again but without success :disappointed_relieved:.

I tried to create it by this:

https://subscription.packtpub.com/book/application-development/9781787123687/3/ch03lvl1sec32/top-level-functions
But without success again :'(. Again: error: cannot find symbol
UploadActivity.text(uploadContext.getString(R.string.Upload_Wait), false,false);

Haha, some files (and classes) was in Java, not in Kotlin - there was a problem. I can generate APK right now. There will not any problem I hope.

Thanks!