Note that there are some explanatory texts on larger screens.

plurals
  1. POfindViewById returns null in custom view
    primarykey
    data
    text
    <p>Ok, I've read around that a custom view must have a constructor with AttributeSet in order for findViewById() to work. I've not seen any actual implementation yet.</p> <p>Below are my codes: My custom view that extends HorizontalScrollView:</p> <pre><code>public AnimatedMenuBar(Context context) { // TODO Auto-generated constructor stub this(context, null); } public AnimatedMenuBar(Context context, AttributeSet attrs) { // TODO Auto-generated constructor stub this(context, attrs, 0); } public AnimatedMenuBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub this.context = context; inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.main, this); this.setHorizontalScrollBarEnabled(false); } </code></pre> <p>From my main activity:</p> <pre><code>@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.testing); AnimatedMenuBar animatedMenuBar = (AnimatedMenuBar)findViewById(R.id.animatedMenuBar); } </code></pre> <p>testing.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"&gt; &lt;com.pt.task.custommenubar.AnimatedMenuBar android:id="@+id/animatedMenuBar" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" &gt; &lt;LinearLayout android:id="@+id/horizontalLayout" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"&gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/dummyTextView" android:padding="5dip"/&gt; &lt;/LinearLayout&gt; &lt;/merge&gt; </code></pre> <p>animatedMenuBar is always null, is there any thing that I missed?</p> <p>Thank you very much!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload