简体   繁体   中英

Android Studio can't see TextView ID

I have a textview, the xml file is

<TextView
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:fontFamily="sans-serif"
        android:lineSpacingExtra="30sp"
        android:text="MyText"
        android:textColor="?android:attr/colorActivatedHighlight"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.22000003" />

There is no ID of this textview so I can't use it in MainActivity.java, I just started Android Studio(I worked with Python so far) what am I missing?

I have also a button and it has an id .

Just add:

<TextView
    android:id="@+id/myTextView"
    ... (rest of lines)
    app:layout_constraintVertical_bias="0.22000003" />

myTextView will be the identifier you choose

Just keep in mind this:

When creating an XML layout file you must specify the Activity name value for the
tools:context like below:

tools:context=".views.QuestionActivity"

This will allow you to retrieve all the ids from the XML layout file in the specified activity.

Also if you have checked that everything is correct but still can't get the ids just restart the IDE.

Make also sure that in your "AndroidManifest.xml" file you have created the activity and assigned the correct name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM