findviewbyid

Why findViewById() is returning null if setcontentview() is not called?

女生的网名这么多〃 提交于 2020-01-09 11:52:29
问题 I am new-bee to android. Here is my xml file - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:id="@+id/linearlayout" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:id="@+id/textview" /> </LinearLayout>

Why findViewById() is returning null if setcontentview() is not called?

狂风中的少年 提交于 2020-01-09 11:51:53
问题 I am new-bee to android. Here is my xml file - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:id="@+id/linearlayout" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:id="@+id/textview" /> </LinearLayout>

Unable to get reference of the items in the Custom ActionBar

房东的猫 提交于 2020-01-04 09:56:50
问题 I am using a RelativeLayout with some items like EditTexts, Buttons to set a Custom ActionBar view. The problem is that I am unable to get the references of the items which are there in that custom view. Here is the custom view for ActionBar <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/actionbar_background" android:gravity="fill_horizontal|center_horizontal" >

Unable to get reference of the items in the Custom ActionBar

有些话、适合烂在心里 提交于 2020-01-04 09:55:50
问题 I am using a RelativeLayout with some items like EditTexts, Buttons to set a Custom ActionBar view. The problem is that I am unable to get the references of the items which are there in that custom view. Here is the custom view for ActionBar <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/actionbar_background" android:gravity="fill_horizontal|center_horizontal" >

Efficiency of findViewById

你。 提交于 2019-12-31 10:39:12
问题 Probably most Android devs know that findViewById is not a cheap operation. Another thing that most of us know, is that you can boost the performance by using the smallest sub-tree of the view hierarchy to find views by their id, example: <LinearLayout android:id="@+id/some_id_0"> <LinearLayout android:id="@+id/some_id_1"> <LinearLayout android:id="@+id/some_id_2"> <TextView android:id="@+id/textview" /> </LinearLayout> </LinearLayout> </LinearLayout> In this case you probably want to search

NullPointerException when binding Views in RecyclerView.Adapter

对着背影说爱祢 提交于 2019-12-31 03:04:19
问题 Why do I get a NullPointerException in my ViewHolder 's bindItems() method? I've highlighted the line where I get the NullPointerException . The blogpost_author ID exists, as you can see in the XML, so what's the problem here? How is findViewById<TextView>(R.id.blogpost_author) returning null? Adapter and ViewHolder code: class BlogPostAdapter(val blogList: ArrayList<BlogPost>) : RecyclerView.Adapter<BlogPostAdapter.ViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType:

FindViewById where ID is dynamic string

[亡魂溺海] 提交于 2019-12-29 05:58:14
问题 I have a table of 40 squares and each has an ID. When I'm passing Bundle from another activity I need to extract Note, Color and ID from that bundle. And then the app will change/add text and change background of the square specified by extracted ID. Each square has ID of int format. Each ID passed from other activity is in string format. I can't figure out how to make it to findViewById(R.id.passed_id), and how to get two different formats working together. I've tried to change ID of each

kotlin get view from NavigationView header

天涯浪子 提交于 2019-12-24 03:59:09
问题 I have such variant, but I believe that there is a better way to do this val header = nav_view.getHeaderView(0) as LinearLayout val userName = header.findViewById(R.id.user_name) as TextView The issue is that when I try to get it just by user_name , it is null . 回答1: This might help you and i think that its better way . nav_view.getHeaderView(0).userNameTxt.text This is shortest way to access from header. 回答2: You might consider using Android-Kotlin extension plugin. In that case your code

Android: why should IDs of views should be positive numbers?

*爱你&永不变心* 提交于 2019-12-24 03:53:39
问题 In View.java, setId - "Sets the identifier for this view. The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number." & findViewById - "Look for a child view with the given id. If this view has the given id, Why I could not use a negative number as the id of the view? I commented out the public final View findViewById(int id) { // The original android check at here // if (id < 0) { // is commented out to test if system could run when Ids

Android: why should IDs of views should be positive numbers?

假如想象 提交于 2019-12-24 03:52:31
问题 In View.java, setId - "Sets the identifier for this view. The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number." & findViewById - "Look for a child view with the given id. If this view has the given id, Why I could not use a negative number as the id of the view? I commented out the public final View findViewById(int id) { // The original android check at here // if (id < 0) { // is commented out to test if system could run when Ids