Reputation: 1025
(Im using Kotlin) So here is my OnCreateView in the Fragment.
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater!!.inflate(R.layout.fragment_bots, container, false)
BotDiv2.visibility = View.VISIBLE
startUp()
return view
}
and here is the xml of the relativelayout:
<RelativeLayout
android:id="@+id/BotDiv2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="33.3"
android:visibility="invisible">
<ImageButton
android:id="@+id/BotBtn1"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@null"
android:scaleType="fitCenter"
android:src="@android:drawable/btn_star_big_on" />
<TextView
android:id="@+id/uselessLevel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/BotBtn1"
android:layout_alignStart="@+id/BotBtn1"
android:text="Level:" />
<TextView
android:id="@+id/BotWorth1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/BotBtn1"
android:layout_centerHorizontal="true"
android:text="$500"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<TextView
android:id="@+id/levelBot1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/BotBtn1"
android:layout_alignEnd="@+id/BotBtn1"
android:text="1" />
</RelativeLayout>
In want to use it in another Function, but this:
BotDiv2.visibility = View.VISIBLE
causes NPEs I also tried to use findViewById, but that causes an NPE as well (or not affecting, cuz of Kotlin's "?").
Upvotes: 0
Views: 83
Reputation: 89548
Other than the ID seemingly not being right, as mentioned in a comment above...
Since at this stage your View
is not set for your Fragment
yet (you haven't returned it to the framework), you can't call findViewById
on the Fragment
itself, but you can make a findViewById
call on the newly inflated View
instead:
val view: View = inflater!!.inflate(R.layout.fragment_bots, container, false)
val bd2 = view.findViewById(R.id.BotDiv2)
bd2.visibility = View.VISIBLE
If you're using Kotlin Android Extensions, you can do the same with this syntax:
val view: View = inflater!!.inflate(R.layout.fragment_bots, container, false)
view.BotDiv2.visibility = View.VISIBLE
Upvotes: 2