[Solved] findViewById returns a null

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

[Solved] findViewById returns a null

Postby Wysie » Wed Dec 16, 2009 6:38 am

Solved: I found the bug. It's because the layout in use was contacts_list_item_photo.xml and not contacts_list_item. *smacks head*

Hi all,

I've made quite a bit of changes to the AOSP Contacts.apk, and my latest attempt was to add the green call button in Call Log into Contacts as well (so I can make a call directly without having to tap on the contact first).

However, I've run into some problems with the following code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public View newView(Context context, Cursor cursor, ViewGroup parent) {
  3.  
  4.             final View view = super.newView(context, cursor, parent);
  5.  
  6.  
  7.  
  8.             final ContactListItemCache cache = new ContactListItemCache();
  9.  
  10.             cache.nameView = (TextView) view.findViewById(R.id.name);
  11.  
  12.             cache.labelView = (TextView) view.findViewById(R.id.label);
  13.  
  14.             cache.numberView = (TextView) view.findViewById(R.id.number);
  15.  
  16.             cache.presenceView = (ImageView) view.findViewById(R.id.presence);
  17.  
  18.             cache.photoView = (ImageView) view.findViewById(R.id.photo);
  19.  
  20.             cache.callView = view.findViewById(R.id.call_icon);
  21.  
  22.            
  23.  
  24.             if (cache.callView == null)
  25.  
  26.                 Log.d("NULL: CALLVIEW", "Why?");
  27.  
  28.            
  29.  
  30.             view.setTag(cache);
  31.  
  32.  
  33.  
  34.             return view;
  35.  
  36.         }
  37.  
  38.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


This is the corresponding XML:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <!--
  5.  
  6. /*
  7.  
  8. * Copyright 2009, The Android Open Source Project
  9.  
  10. *
  11.  
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13.  
  14. * you may not use this file except in compliance with the License.
  15.  
  16. * You may obtain a copy of the License at
  17.  
  18. *
  19.  
  20. *      http://www.apache.org/licenses/LICENSE-2.0
  21.  
  22. *
  23.  
  24. * Unless required by applicable law or agreed to in writing, software
  25.  
  26. * distributed under the License is distributed on an "AS IS" BASIS,
  27.  
  28. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  29.  
  30. * See the License for the specific language governing permissions and
  31.  
  32. * limitations under the License.
  33.  
  34. */
  35.  
  36. -->
  37.  
  38.  
  39.  
  40. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  41.  
  42.    android:layout_width="fill_parent"
  43.  
  44.    android:layout_height="?android:attr/listPreferredItemHeight"
  45.  
  46.    android:paddingLeft="7dip"
  47.  
  48.    android:paddingRight="5dip"
  49.  
  50. >
  51.  
  52.     <ImageView android:id="@+id/presence"
  53.  
  54.        android:layout_width="32dip"
  55.  
  56.        android:layout_height="32dip"
  57.  
  58.        android:layout_alignParentRight="true"
  59.  
  60.        android:layout_marginLeft="5dip"
  61.  
  62.        android:layout_centerVertical="true"
  63.  
  64.  
  65.  
  66.        android:gravity="center"
  67.  
  68.        android:scaleType="centerInside"
  69.  
  70.    />
  71.  
  72.  
  73.  
  74.     <TextView android:id="@+id/label"
  75.  
  76.        android:layout_width="wrap_content"
  77.  
  78.        android:layout_height="wrap_content"
  79.  
  80.        android:layout_alignParentLeft="true"
  81.  
  82.        android:layout_alignParentBottom="true"
  83.  
  84.        android:layout_marginBottom="8dip"
  85.  
  86.        android:layout_marginTop="-8dip"
  87.  
  88.  
  89.  
  90.        android:singleLine="true"
  91.  
  92.        android:textAppearance="?android:attr/textAppearanceSmall"
  93.  
  94.        android:textStyle="bold"
  95.  
  96.    />
  97.  
  98.  
  99.  
  100.     <TextView android:id="@+id/number"
  101.  
  102.        android:layout_width="wrap_content"
  103.  
  104.        android:layout_height="wrap_content"
  105.  
  106.        android:layout_marginLeft="5dip"
  107.  
  108.        android:layout_toRightOf="@id/label"
  109.  
  110.        android:layout_alignBaseline="@id/label"
  111.  
  112.        android:layout_toLeftOf="@id/presence"
  113.  
  114.        android:layout_alignWithParentIfMissing="true"
  115.  
  116.  
  117.  
  118.        android:singleLine="true"
  119.  
  120.        android:ellipsize="marquee"
  121.  
  122.        android:textAppearance="?android:attr/textAppearanceSmall"
  123.  
  124.    />
  125.  
  126.  
  127.  
  128.     <TextView android:id="@+id/name"
  129.  
  130.        android:layout_width="0dip"
  131.  
  132.        android:layout_height="0dip"
  133.  
  134.        android:layout_above="@id/label"
  135.  
  136.        android:layout_alignWithParentIfMissing="true"
  137.  
  138.        android:layout_alignParentTop="true"
  139.  
  140.        android:layout_alignParentLeft="true"
  141.  
  142.        android:layout_toLeftOf="@id/presence"
  143.  
  144.        android:layout_marginBottom="1dip"
  145.  
  146.  
  147.  
  148.        android:singleLine="true"
  149.  
  150.        android:ellipsize="marquee"
  151.  
  152.        android:gravity="center_vertical|left"
  153.  
  154.        android:textAppearance="?android:attr/textAppearanceLarge"
  155.  
  156.    />
  157.  
  158.    
  159.  
  160.     <com.android.contacts.ui.widget.DontPressWithParentImageView
  161.  
  162.         android:id="@+id/call_icon"
  163.  
  164.        android:layout_width="wrap_content"
  165.  
  166.        android:layout_height="fill_parent"
  167.  
  168.        android:paddingLeft="14dip"
  169.  
  170.        android:paddingRight="14dip"
  171.  
  172.        android:layout_alignParentRight="true"
  173.  
  174.  
  175.  
  176.        android:gravity="center_vertical"
  177.  
  178.        android:src="@android:drawable/sym_action_call"
  179.  
  180.        android:background="@drawable/call_background"
  181.  
  182.    />
  183.  
  184.  
  185.  
  186.     <View android:id="@+id/divider"
  187.  
  188.        android:layout_width="1px"
  189.  
  190.        android:layout_height="fill_parent"
  191.  
  192.        android:layout_marginTop="5dip"
  193.  
  194.        android:layout_marginBottom="5dip"
  195.  
  196.        android:layout_toLeftOf="@id/call_icon"
  197.  
  198.        android:layout_marginLeft="11dip"
  199.  
  200.        android:background="@drawable/divider_vertical_dark"
  201.  
  202.    />
  203.  
  204.  
  205.  
  206. </RelativeLayout>
  207.  
  208.  
Parsed in 0.009 seconds, using GeSHi 1.0.8.4


For some reason, every single view returns something, but cache.callView is NULL :(. My source can be found here http://github.com/Wysie (which does not include this yet). The problem should not be setContentView as earlier in the code there's this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  public ContactItemListAdapter(Context context) {
  3.  
  4.             super(ContactsListActivity.this, R.layout.contacts_list_item, null, false);
  5.  
  6.  
  7.  
  8.             mAlphabet = context.getString(com.android.internal.R.string.fast_scroll_alphabet);
  9.  
  10.  
  11.  
  12.             mUnknownNameText = context.getText(android.R.string.unknownName);
  13.  
  14.             switch (mMode) {
  15.  
  16.                 case MODE_PICK_POSTAL:
  17.  
  18.                     mLocalizedLabels = EditContactActivity.getLabelsForKind(mContext,
  19.  
  20.                             Contacts.KIND_POSTAL);
  21.  
  22.                     break;
  23.  
  24.                 default:
  25.  
  26.                     mLocalizedLabels = EditContactActivity.getLabelsForKind(mContext,
  27.  
  28.                             Contacts.KIND_PHONE);
  29.  
  30.                     break;
  31.  
  32.             }
  33.  
  34.  
  35.  
  36.             if ((mMode & MODE_MASK_SHOW_PHOTOS) == MODE_MASK_SHOW_PHOTOS) {
  37.  
  38.                 mDisplayPhotos = true;
  39.  
  40.                 setViewResource(R.layout.contacts_list_item_photo);
  41.  
  42.                 mBitmapCache = new SparseArray<SoftReference<Bitmap>>();
  43.  
  44.             }
  45.  
  46.         }
  47.  
  48.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Moreover, every other View is not null, except for callView. I've also attached the full source with this bug here. The file of concern is ContactsListActivity.java. Thanks for the help :)!
Attachments
Contacts_with_bug.zip
Full Source With Bug (Check ContactsListActivity.java)
(1.84 MiB) Downloaded 38 times
Wysie
Freshman
Freshman
 
Posts: 4
Joined: Wed Dec 16, 2009 6:26 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests