Kotlin-Android First App Unresolved Reference TextView Button etc
I'm new to Android & Kotlin development.
I wanted to get started with a simple "Hello World", but am already running into problems.
I added a Textview to my MainActivity and want to set an onClick listener to change the text of a TextView I dragged into the activity.
The compiler is now complaining that 'TextView' is an unresolved reference (it does the same with Buttons etc.).
I then added a kotlinx import as suggested by a website, but this fails to solve anything. Code sample below, anything with an asterisk as a line comment was added by me.
package com.example.my.mynewapp
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.fragmentX.view.* // *
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView: TextView = findViewById(R.id.testView) as TextView // *
textView.setOnClickListener { // *
textView.text = "You clicked me! You flipping clicked me!" // *
} // *
}
}
Would anyone have any idea what's going on?
android kotlin textview
add a comment |
I'm new to Android & Kotlin development.
I wanted to get started with a simple "Hello World", but am already running into problems.
I added a Textview to my MainActivity and want to set an onClick listener to change the text of a TextView I dragged into the activity.
The compiler is now complaining that 'TextView' is an unresolved reference (it does the same with Buttons etc.).
I then added a kotlinx import as suggested by a website, but this fails to solve anything. Code sample below, anything with an asterisk as a line comment was added by me.
package com.example.my.mynewapp
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.fragmentX.view.* // *
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView: TextView = findViewById(R.id.testView) as TextView // *
textView.setOnClickListener { // *
textView.text = "You clicked me! You flipping clicked me!" // *
} // *
}
}
Would anyone have any idea what's going on?
android kotlin textview
1
Just solved it, apparently you have to import android.widget.TextView, which solves the issue. No need to downvote, we all have to learn.
– The Fox
Nov 25 '18 at 18:24
no need to use findViewById in kotlin. you can directly access your view using id.
– Akshay Raiyani
Nov 26 '18 at 7:42
add a comment |
I'm new to Android & Kotlin development.
I wanted to get started with a simple "Hello World", but am already running into problems.
I added a Textview to my MainActivity and want to set an onClick listener to change the text of a TextView I dragged into the activity.
The compiler is now complaining that 'TextView' is an unresolved reference (it does the same with Buttons etc.).
I then added a kotlinx import as suggested by a website, but this fails to solve anything. Code sample below, anything with an asterisk as a line comment was added by me.
package com.example.my.mynewapp
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.fragmentX.view.* // *
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView: TextView = findViewById(R.id.testView) as TextView // *
textView.setOnClickListener { // *
textView.text = "You clicked me! You flipping clicked me!" // *
} // *
}
}
Would anyone have any idea what's going on?
android kotlin textview
I'm new to Android & Kotlin development.
I wanted to get started with a simple "Hello World", but am already running into problems.
I added a Textview to my MainActivity and want to set an onClick listener to change the text of a TextView I dragged into the activity.
The compiler is now complaining that 'TextView' is an unresolved reference (it does the same with Buttons etc.).
I then added a kotlinx import as suggested by a website, but this fails to solve anything. Code sample below, anything with an asterisk as a line comment was added by me.
package com.example.my.mynewapp
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.fragmentX.view.* // *
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView: TextView = findViewById(R.id.testView) as TextView // *
textView.setOnClickListener { // *
textView.text = "You clicked me! You flipping clicked me!" // *
} // *
}
}
Would anyone have any idea what's going on?
android kotlin textview
android kotlin textview
asked Nov 25 '18 at 18:13
The FoxThe Fox
458
458
1
Just solved it, apparently you have to import android.widget.TextView, which solves the issue. No need to downvote, we all have to learn.
– The Fox
Nov 25 '18 at 18:24
no need to use findViewById in kotlin. you can directly access your view using id.
– Akshay Raiyani
Nov 26 '18 at 7:42
add a comment |
1
Just solved it, apparently you have to import android.widget.TextView, which solves the issue. No need to downvote, we all have to learn.
– The Fox
Nov 25 '18 at 18:24
no need to use findViewById in kotlin. you can directly access your view using id.
– Akshay Raiyani
Nov 26 '18 at 7:42
1
1
Just solved it, apparently you have to import android.widget.TextView, which solves the issue. No need to downvote, we all have to learn.
– The Fox
Nov 25 '18 at 18:24
Just solved it, apparently you have to import android.widget.TextView, which solves the issue. No need to downvote, we all have to learn.
– The Fox
Nov 25 '18 at 18:24
no need to use findViewById in kotlin. you can directly access your view using id.
– Akshay Raiyani
Nov 26 '18 at 7:42
no need to use findViewById in kotlin. you can directly access your view using id.
– Akshay Raiyani
Nov 26 '18 at 7:42
add a comment |
1 Answer
1
active
oldest
votes
You are inflating activity_main.xml
in your class.
Does this TextView
belong to the above layout?
If it does then you don't need findViewById()
Just add to your imports:
import kotlinx.android.synthetic.main.activity_main.*
and not:
import kotlinx.android.synthetic.main.fragmentX.view.*
then use testView
(this is the id of the TextView
unless it's a typo) anywhere in your activity class.
Yes, the TextView named 'testView' belongs to the layout. Adding your import does nothing; I can only compile when I explicitly add 'import android.widgets.TextView'. What do you mean with 'inflating' the xml-file in my class?
– The Fox
Nov 25 '18 at 18:38
@TheFox if this is the case then there is a problem because you should import all the views of the layout 1 by 1. But this is not the case with Kotlin. One advantage of using Kotlin is that you don't need findViewById() inside an activity to access its views. You just import the layout like I did and that' it. If after that the TextView is not recognized then it does not belong toactivity_main.xml
, or maybe you need to clean and rebuild your project.
– forpas
Nov 25 '18 at 18:43
for all clarity, I have to import android.widgets.TextView, don't I? Cleaning and rebuilding doesn't solve the issue, activity_main.xml does contain the TextViews however.
– The Fox
Nov 25 '18 at 18:55
android.widget.TextView
is autiimported in any class that uses in its code a TextView. You don't have to import it. Even if you write something likeval tv: TextView...
AS will importandroid.widget.TextView
or you will by pressing Alt Enter and accepting the suggestion.
– forpas
Nov 25 '18 at 19:09
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470434%2fkotlin-android-first-app-unresolved-reference-textview-button-etc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are inflating activity_main.xml
in your class.
Does this TextView
belong to the above layout?
If it does then you don't need findViewById()
Just add to your imports:
import kotlinx.android.synthetic.main.activity_main.*
and not:
import kotlinx.android.synthetic.main.fragmentX.view.*
then use testView
(this is the id of the TextView
unless it's a typo) anywhere in your activity class.
Yes, the TextView named 'testView' belongs to the layout. Adding your import does nothing; I can only compile when I explicitly add 'import android.widgets.TextView'. What do you mean with 'inflating' the xml-file in my class?
– The Fox
Nov 25 '18 at 18:38
@TheFox if this is the case then there is a problem because you should import all the views of the layout 1 by 1. But this is not the case with Kotlin. One advantage of using Kotlin is that you don't need findViewById() inside an activity to access its views. You just import the layout like I did and that' it. If after that the TextView is not recognized then it does not belong toactivity_main.xml
, or maybe you need to clean and rebuild your project.
– forpas
Nov 25 '18 at 18:43
for all clarity, I have to import android.widgets.TextView, don't I? Cleaning and rebuilding doesn't solve the issue, activity_main.xml does contain the TextViews however.
– The Fox
Nov 25 '18 at 18:55
android.widget.TextView
is autiimported in any class that uses in its code a TextView. You don't have to import it. Even if you write something likeval tv: TextView...
AS will importandroid.widget.TextView
or you will by pressing Alt Enter and accepting the suggestion.
– forpas
Nov 25 '18 at 19:09
add a comment |
You are inflating activity_main.xml
in your class.
Does this TextView
belong to the above layout?
If it does then you don't need findViewById()
Just add to your imports:
import kotlinx.android.synthetic.main.activity_main.*
and not:
import kotlinx.android.synthetic.main.fragmentX.view.*
then use testView
(this is the id of the TextView
unless it's a typo) anywhere in your activity class.
Yes, the TextView named 'testView' belongs to the layout. Adding your import does nothing; I can only compile when I explicitly add 'import android.widgets.TextView'. What do you mean with 'inflating' the xml-file in my class?
– The Fox
Nov 25 '18 at 18:38
@TheFox if this is the case then there is a problem because you should import all the views of the layout 1 by 1. But this is not the case with Kotlin. One advantage of using Kotlin is that you don't need findViewById() inside an activity to access its views. You just import the layout like I did and that' it. If after that the TextView is not recognized then it does not belong toactivity_main.xml
, or maybe you need to clean and rebuild your project.
– forpas
Nov 25 '18 at 18:43
for all clarity, I have to import android.widgets.TextView, don't I? Cleaning and rebuilding doesn't solve the issue, activity_main.xml does contain the TextViews however.
– The Fox
Nov 25 '18 at 18:55
android.widget.TextView
is autiimported in any class that uses in its code a TextView. You don't have to import it. Even if you write something likeval tv: TextView...
AS will importandroid.widget.TextView
or you will by pressing Alt Enter and accepting the suggestion.
– forpas
Nov 25 '18 at 19:09
add a comment |
You are inflating activity_main.xml
in your class.
Does this TextView
belong to the above layout?
If it does then you don't need findViewById()
Just add to your imports:
import kotlinx.android.synthetic.main.activity_main.*
and not:
import kotlinx.android.synthetic.main.fragmentX.view.*
then use testView
(this is the id of the TextView
unless it's a typo) anywhere in your activity class.
You are inflating activity_main.xml
in your class.
Does this TextView
belong to the above layout?
If it does then you don't need findViewById()
Just add to your imports:
import kotlinx.android.synthetic.main.activity_main.*
and not:
import kotlinx.android.synthetic.main.fragmentX.view.*
then use testView
(this is the id of the TextView
unless it's a typo) anywhere in your activity class.
answered Nov 25 '18 at 18:25
forpasforpas
17k3628
17k3628
Yes, the TextView named 'testView' belongs to the layout. Adding your import does nothing; I can only compile when I explicitly add 'import android.widgets.TextView'. What do you mean with 'inflating' the xml-file in my class?
– The Fox
Nov 25 '18 at 18:38
@TheFox if this is the case then there is a problem because you should import all the views of the layout 1 by 1. But this is not the case with Kotlin. One advantage of using Kotlin is that you don't need findViewById() inside an activity to access its views. You just import the layout like I did and that' it. If after that the TextView is not recognized then it does not belong toactivity_main.xml
, or maybe you need to clean and rebuild your project.
– forpas
Nov 25 '18 at 18:43
for all clarity, I have to import android.widgets.TextView, don't I? Cleaning and rebuilding doesn't solve the issue, activity_main.xml does contain the TextViews however.
– The Fox
Nov 25 '18 at 18:55
android.widget.TextView
is autiimported in any class that uses in its code a TextView. You don't have to import it. Even if you write something likeval tv: TextView...
AS will importandroid.widget.TextView
or you will by pressing Alt Enter and accepting the suggestion.
– forpas
Nov 25 '18 at 19:09
add a comment |
Yes, the TextView named 'testView' belongs to the layout. Adding your import does nothing; I can only compile when I explicitly add 'import android.widgets.TextView'. What do you mean with 'inflating' the xml-file in my class?
– The Fox
Nov 25 '18 at 18:38
@TheFox if this is the case then there is a problem because you should import all the views of the layout 1 by 1. But this is not the case with Kotlin. One advantage of using Kotlin is that you don't need findViewById() inside an activity to access its views. You just import the layout like I did and that' it. If after that the TextView is not recognized then it does not belong toactivity_main.xml
, or maybe you need to clean and rebuild your project.
– forpas
Nov 25 '18 at 18:43
for all clarity, I have to import android.widgets.TextView, don't I? Cleaning and rebuilding doesn't solve the issue, activity_main.xml does contain the TextViews however.
– The Fox
Nov 25 '18 at 18:55
android.widget.TextView
is autiimported in any class that uses in its code a TextView. You don't have to import it. Even if you write something likeval tv: TextView...
AS will importandroid.widget.TextView
or you will by pressing Alt Enter and accepting the suggestion.
– forpas
Nov 25 '18 at 19:09
Yes, the TextView named 'testView' belongs to the layout. Adding your import does nothing; I can only compile when I explicitly add 'import android.widgets.TextView'. What do you mean with 'inflating' the xml-file in my class?
– The Fox
Nov 25 '18 at 18:38
Yes, the TextView named 'testView' belongs to the layout. Adding your import does nothing; I can only compile when I explicitly add 'import android.widgets.TextView'. What do you mean with 'inflating' the xml-file in my class?
– The Fox
Nov 25 '18 at 18:38
@TheFox if this is the case then there is a problem because you should import all the views of the layout 1 by 1. But this is not the case with Kotlin. One advantage of using Kotlin is that you don't need findViewById() inside an activity to access its views. You just import the layout like I did and that' it. If after that the TextView is not recognized then it does not belong to
activity_main.xml
, or maybe you need to clean and rebuild your project.– forpas
Nov 25 '18 at 18:43
@TheFox if this is the case then there is a problem because you should import all the views of the layout 1 by 1. But this is not the case with Kotlin. One advantage of using Kotlin is that you don't need findViewById() inside an activity to access its views. You just import the layout like I did and that' it. If after that the TextView is not recognized then it does not belong to
activity_main.xml
, or maybe you need to clean and rebuild your project.– forpas
Nov 25 '18 at 18:43
for all clarity, I have to import android.widgets.TextView, don't I? Cleaning and rebuilding doesn't solve the issue, activity_main.xml does contain the TextViews however.
– The Fox
Nov 25 '18 at 18:55
for all clarity, I have to import android.widgets.TextView, don't I? Cleaning and rebuilding doesn't solve the issue, activity_main.xml does contain the TextViews however.
– The Fox
Nov 25 '18 at 18:55
android.widget.TextView
is autiimported in any class that uses in its code a TextView. You don't have to import it. Even if you write something like val tv: TextView...
AS will import android.widget.TextView
or you will by pressing Alt Enter and accepting the suggestion.– forpas
Nov 25 '18 at 19:09
android.widget.TextView
is autiimported in any class that uses in its code a TextView. You don't have to import it. Even if you write something like val tv: TextView...
AS will import android.widget.TextView
or you will by pressing Alt Enter and accepting the suggestion.– forpas
Nov 25 '18 at 19:09
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470434%2fkotlin-android-first-app-unresolved-reference-textview-button-etc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Just solved it, apparently you have to import android.widget.TextView, which solves the issue. No need to downvote, we all have to learn.
– The Fox
Nov 25 '18 at 18:24
no need to use findViewById in kotlin. you can directly access your view using id.
– Akshay Raiyani
Nov 26 '18 at 7:42