- 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
20 odpowiedzi
+ 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.
+ 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.
+ 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
}
})
}
}
+ 1
OK. Thank you for your help.
+ 1
Errorguy hello
You shouldn't return 2 or more times
So delete the return on the top
```kt
return inflater.inflate...
```
THIS code '
+ 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
}
```
+ 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
0
you forgot to declare a variable btn_lockitem in your code.
0
How do I do that, Rohit ?
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
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?
0
No. And it doesn't open a fragment
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.
0
No. I don't have any tutorials
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
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.
0
"View" in "return view" is an unresolved reference now.
0
Type mismatch. Required: unit
Found: View?
https://www.sololearn.com/post/1042285/?ref=app
0
what about the 2nd answer answer in this link
https://stackoverflow.com/questions/60565585/how-to-get-a-button-id-in-of-a-clicked-button
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