Unresolved reference btn_lockitem - please help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Unresolved reference btn_lockitem - please help

Dear Community I want to make an android app with Android studio. Now, I have a button with the itemid "btn_lockitem" in the main.xml file, with which the user should open fragment_lockscreen.xml Now my question: Why is there an unresolved reference for my btn_lockitem button? Thank you for any help Kind regards Errorguy https://www.sololearn.com/post/1042211/?ref=app https://www.sololearn.com/post/1042212/?ref=app

17th Apr 2021, 8:05 PM
Errorguy
20 Answers
+ 2
I'm really sorry, it's very difficult to know what the real issues are, remotely. But don't give up keep finding the solution, but if you still can't then I think it's better to follow some tutorials first which I am sure you can find in youtube. And then try again to implement it on your own.
17th Apr 2021, 9:14 PM
Rohit Kh
Rohit Kh - avatar
+ 1
declare a local variable "btn_lockitem" of whatever data type it is inside your function at the top. Or read the error msg you are getting in Error view 1 and follow the IDE instruction that is press Alt+Umschalt+Eingabe to automatically declare and create a variable bin_lockitem for you.
17th Apr 2021, 8:19 PM
Rohit Kh
Rohit Kh - avatar
+ 1
something like this: class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.fragment_lockscreen) val button = findViewById<Button>(R.id.btn_lockitem) button.setOnClickListener(object : View.OnClickListener { override fun onClick(v: View?) { return view; // Code here executes on main thread after user presses button } }) } }
17th Apr 2021, 8:50 PM
Rohit Kh
Rohit Kh - avatar
+ 1
OK. Thank you for your help.
17th Apr 2021, 9:15 PM
Errorguy
+ 1
Errorguy hello You shouldn't return 2 or more times So delete the return on the top ```kt return inflater.inflate... ``` THIS code '
18th Apr 2021, 5:10 PM
Ananiya Jemberu
Ananiya Jemberu - avatar
+ 1
And the other Writing this much code super duper useless ```kt button.setOnClickListener(object : View.onClickL... ``` Write like ```kt button.setOnClickListener{ // your code goes here and returning view is useless } ```
18th Apr 2021, 5:15 PM
Ananiya Jemberu
Ananiya Jemberu - avatar
+ 1
'Rohit like this? Now the error is gone but the Code is unreachable. Any idea? Sry, I am a total beginner. https://www.sololearn.com/post/1042251/?ref=app ' Well remove the return above, adding 2 or more return makes the bottom code unreachable
18th Apr 2021, 5:19 PM
Ananiya Jemberu
Ananiya Jemberu - avatar
0
you forgot to declare a variable btn_lockitem in your code.
17th Apr 2021, 8:13 PM
Rohit Kh
Rohit Kh - avatar
0
How do I do that, Rohit ?
17th Apr 2021, 8:14 PM
Errorguy
0
Rohit like this? Now the error is gone but the Code is unreachable. Any idea? Sry, I am a total beginner. https://www.sololearn.com/post/1042251/?ref=app
17th Apr 2021, 8:34 PM
Errorguy
0
Check what happen when you click on the button. According to your code it's should output some log thing. Do you see anything like log being printed?
17th Apr 2021, 8:36 PM
Rohit Kh
Rohit Kh - avatar
0
No. And it doesn't open a fragment
17th Apr 2021, 8:37 PM
Errorguy
0
Then what's the issue... its very difficult to debug things like this remotely. Are you following or not some tutorials, if no then I would recommend you to because these things need proper understanding and help when starting new. Coming to the problem : you are missing something on the fragment thing like wrong id.
17th Apr 2021, 8:42 PM
Rohit Kh
Rohit Kh - avatar
0
No. I don't have any tutorials
17th Apr 2021, 8:44 PM
Errorguy
0
Ah, I think you must return view when you click on the button, right? Errorguy put "return view" inside the btn_lockitem.onclick... function once and try again
17th Apr 2021, 8:45 PM
Rohit Kh
Rohit Kh - avatar
0
My Code now looks like this: class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.fragment_lockscreen) val button = findViewById<Button>(R.id.btn_lockitem) button.setOnClickListener(object : View.OnClickListener { override fun onClick(v: View?) { // Code here executes on main thread after user presses button } }) } } Where do I need to implement the return view? Your Code was translated to kotlin automatically.
17th Apr 2021, 8:48 PM
Errorguy
0
"View" in "return view" is an unresolved reference now.
17th Apr 2021, 8:51 PM
Errorguy
0
Type mismatch. Required: unit Found: View? https://www.sololearn.com/post/1042285/?ref=app
17th Apr 2021, 8:56 PM
Errorguy
17th Apr 2021, 9:01 PM
Rohit Kh
Rohit Kh - avatar
0
Too much errors. The last code was better. But why the hell is there a type mismatch? It found the view tag which is pointed to the fragment. Why it doesn't work? I don't understand
17th Apr 2021, 9:07 PM
Errorguy