Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] Android Java: How to get component in a custom view? (Trying to access components via findByViewId and get null)

Posted on November 11, 2022 By

Solution 1 :

1)Don’t call views Fragments. Fragments are a separate concept in Android, and your code will confuse everyone.

2)If you created that view as a subview of your custom view, you’d use findViewById, not context.findViewById. Because the view isn’t in the context, its one of your subviews.

Solution 2 :

Thanks to all!!!
I’ve found a way to connect the xml to the View class:

ayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.fragment_players, null, false);

Problem :

Trying to access components via findByViewId and get null
The problem is in this line:

(I cannot connect my viewxml to the class)

// HERE I GET NULL
    **_addButton     = context.findViewById(R.id.buttonAdd);**

I have an activity:
with this OnCreate:

MainActivity class

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
}

I have a custom view I call in my MainActivity
MainActivity xml

   <com.example.myemptyapplication.PlayersFragment
        android_id="@+id/fragment_container_view_players"
        android_layout_weight="1"
        android_layout_width="match_parent"
        android_layout_height="match_parent"
        android_layout_marginLeft="10px"/>

In ‘PlayersFragment’ I’ trying to access

PlayersFragment extends View
@RequiresApi(api = Build.VERSION_CODES.O)
    public PlayersFragment(Context context, AttributeSet attSet) {
        super(context, attSet);
 protected void onFinishInflate() {
        super.onFinishInflate();
    // HERE I GET NULL
        **_addButton     = context.findViewById(R.id.buttonAdd);**
    }

Tried to get components in a custom view

Comments

Comment posted by CommonsWare

There is no sign of anything named

Comment posted by Bracadabra

What is context in your snippet? If it is Android context, then there is no findViewById method.

Comment posted by Tammy Roytvarf

buttonAdd this commin from an XML:

Comment posted by Tammy Roytvarf

Thanks! (2) this actually what what actually worked.

Comment posted by Gabe Sechan

@TammyRoytvarf 1 wasn’t a way to solve it , it was advice. Calling them Fragments when Fragments are a separate major concept in Android confuses everyone who reads the code.

READ  [FIXED] java - setOnItemClickListener don't work on ListView in Fragment
Powered by Inline Related Posts
Android Tags:android, android-components, android-custom-view, java

Post navigation

Previous Post: [FIXED] android – LottieAnimationView doesn’t show in my_layout.xml
Next Post: [FIXED] android – Firestore conditional array query

Related Posts

[FIXED] java – app getting crashed after adding onclick to the buttons Android
[FIXED] animation – Is android:windowExitAnimation not working? Android
[FIXED] kotlin – Android FatalException by pressing FloatingActionButton Android
[FIXED] android – Problem pulling data from GetIt to stream builder Android
[FIXED] node.js – I want to select a random XPath from the defined Array Android
[FIXED] kotlin – android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: Model.uuuid (code 1555 SQLITE_CONSTRAINT_PRIMARYKEY) Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Which is stronger 19 gauge or 23 gauge?
  • Does BMW still use GM transmissions?
  • Is primary or secondary market research better?
  • What does it mean when ADT says low battery?
  • Can a baby sleep on a regular mattress?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme