Getting contacts with numbers into a listview

Put your problem here if it does not fit any of the other categories.

Getting contacts with numbers into a listview

Postby SeraphimSerapis » Mon Dec 07, 2009 4:29 pm

Hello there,
I need a feature in an app, where people can choose a contact from an list as number1 or number2.

But sometimes I get a Nullpointer Exception and don't quite know why..
I thought it's because there is no default number, but even when i set them, the exception is caused.


Can someone help me changing the code?
I only want contacts with numbers (and perhaps some advise how to get the standard-number)


This is the code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private static String[] PROJECTION = new String[] {
  2.  
  3.                 People._ID,
  4.  
  5.                 People.NAME,
  6.  
  7.                 People.NUMBER
  8.  
  9.             };
  10.  
  11.            
  12.  
  13.             private int mPhoneColumnIndex;
  14.  
  15.             private TextView mPhone;
  16.  
  17.                 private String lastSel="";
  18.  
  19.                 private final int NUMBER1=1;
  20.  
  21.                 private final int NUMBER2=2;
  22.  
  23.                 SharedPreferences pref;
  24.  
  25.  
  26.  
  27.             @Override
  28.  
  29.             protected void onCreate(Bundle savedInstanceState) {
  30.  
  31.                 super.onCreate(savedInstanceState);
  32.  
  33.                 setContentView(R.layout.list);
  34.  
  35.                 mPhone = (TextView) findViewById(R.id.phone);
  36.  
  37.                 getListView().setOnItemSelectedListener(this);
  38.  
  39.                 pref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
  40.  
  41.                    
  42.  
  43.                
  44.  
  45.                 //Cursor mit allen Kontakten
  46.  
  47.                 Cursor c = getContentResolver().query(People.CONTENT_URI, PROJECTION, null, null, null);
  48.  
  49.                
  50.  
  51.                 startManagingCursor(c);
  52.  
  53.                 mPhoneColumnIndex = c.getColumnIndex(People.NUMBER);
  54.  
  55.  
  56.  
  57.                 ListAdapter adapter = new SimpleCursorAdapter(this,
  58.  
  59.                         android.R.layout.simple_list_item_1,
  60.  
  61.                         c,
  62.  
  63.                         new String[] {People.NAME},
  64.  
  65.                         new int[] {android.R.id.text1}
  66.  
  67.                 );
  68.  
  69.                
  70.  
  71.                 getListView().setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
  72.  
  73.                             public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
  74.  
  75.                                         menu.add(0, NUMBER1, 0, "Number 1");
  76.  
  77.                                         menu.add(0, NUMBER2, 0, "Number 2");
  78.  
  79.                                     }
  80.  
  81.                 });
  82.  
  83.                 setListAdapter(adapter);
  84.  
  85.             }
  86.  
  87.  
  88.  
  89.             public void onItemSelected(AdapterView parent, View v, int position, long id) {
  90.  
  91.                 if (position >= 0) {
  92.  
  93.                     Cursor c = (Cursor) parent.getItemAtPosition(position);
  94.  
  95.                     mPhone.setText(c.getString(mPhoneColumnIndex));
  96.  
  97.                     lastSel = c.getString(mPhoneColumnIndex);
  98.  
  99.                 }
  100.  
  101.             }
  102.  
  103.            
  104.  
  105.             public void onListItemClick(ListView parent, View v, int position, long id) {
  106.  
  107.                 if (position >= 0) {
  108.  
  109.                     Cursor c = (Cursor) parent.getItemAtPosition(position);
  110.  
  111.                     mPhone.setText(c.getString(mPhoneColumnIndex));
  112.  
  113.                     lastSel = c.getString(mPhoneColumnIndex);
  114.  
  115.                 }
  116.  
  117.             }
  118.  
  119.            
  120.  
  121.             public boolean onContextItemSelected(MenuItem item) {
  122.  
  123.                   AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
  124.  
  125.                   Cursor c = (Cursor) getListView().getItemAtPosition(info.position);
  126.  
  127.                   String cNumber=null;
  128.  
  129.                   switch (item.getItemId()) {
  130.  
  131.                         case NUMBER1:
  132.  
  133.                                 try {
  134.  
  135.                                   cNumber = c.getString(mPhoneColumnIndex);
  136.  
  137.                                   pref.edit().putString("number1",cNumber).commit();
  138.  
  139.                                   pref.edit().putString("name1", ((TextView)getListView().getChildAt(info.position)).getText().toString()).commit();
  140.  
  141.                                 } catch (Exception e) {
  142.  
  143.                                         Toast.makeText(getBaseContext(), "Error: "+e, Toast.LENGTH_LONG).show();
  144.  
  145.                                 }
  146.  
  147.                            
  148.  
  149.                             //Toast.makeText(getBaseContext(), "Name "+((TextView)getListView().getChildAt(info.position)).getText().toString()+" Nummer "+c.getString(mPhoneColumnIndex), Toast.LENGTH_LONG).show();
  150.  
  151.                             return true;
  152.  
  153.                         case NUMBER2:
  154.  
  155.                                 try {
  156.  
  157.                                           cNumber = c.getString(mPhoneColumnIndex);
  158.  
  159.                                           pref.edit().putString("number2",cNumber).commit();
  160.  
  161.                                           pref.edit().putString("name2", ((TextView)getListView().getChildAt(info.position)).getText().toString()).commit();
  162.  
  163.                                 } catch (Exception e) {
  164.  
  165.                                                 Toast.makeText(getBaseContext(), "Error: "+e, Toast.LENGTH_LONG).show();
  166.  
  167.                                 }
  168.  
  169.                                 //Toast.makeText(getBaseContext(), "Name "+((TextView)getListView().getChildAt(info.position)).getText().toString()+" Nummer "+c.getString(mPhoneColumnIndex), Toast.LENGTH_LONG).show();
  170.  
  171.                             return true;
  172.  
  173.                         default:
  174.  
  175.                                 return super.onContextItemSelected(item);
  176.  
  177.                   }
  178.  
  179.             }
  180.  
  181.  
  182.  
  183.             public void onNothingSelected(AdapterView parent) {
  184.  
  185.                 mPhone.setText(R.string.longPress);
  186.  
  187.             }
Parsed in 0.045 seconds, using GeSHi 1.0.8.4



Regards,

Tim
SeraphimSerapis
Junior Developer
Junior Developer
 
Posts: 14
Joined: Wed Mar 11, 2009 10:57 pm

Top

Postby qlimax » Mon Dec 07, 2009 4:47 pm

can you post the logcat error pls :?:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby SeraphimSerapis » Mon Dec 07, 2009 4:56 pm

12-07 16:56:14.225: DEBUG/Exception Nr. 1(29252): java.lang.NullPointerException
12-07 16:56:48.632: DEBUG/Exception Nr. 2(29252): java.lang.NullPointerException


This is what i get from logcat
SeraphimSerapis
Junior Developer
Junior Developer
 
Posts: 14
Joined: Wed Mar 11, 2009 10:57 pm

Postby qlimax » Tue Dec 08, 2009 12:50 am

SeraphimSerapis wrote:12-07 16:56:14.225: DEBUG/Exception Nr. 1(29252): java.lang.NullPointerException
12-07 16:56:48.632: DEBUG/Exception Nr. 2(29252): java.lang.NullPointerException


This is what i get from logcat


it's hard to find the error with so much indications... :P
I suggest you to use try-catch blocks to manage eventual exceptions... maybe also some debug Log.d(....)
sorry I can't do more... :roll:
bye
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby SeraphimSerapis » Tue Dec 08, 2009 9:55 am

One thing.. i do get these errors only on 2.0 emulators or phones like the milestone or droid.

Is there a way to do it compatible to 2.0?

ContactsContract is only 2.0, isn't it?
SeraphimSerapis
Junior Developer
Junior Developer
 
Posts: 14
Joined: Wed Mar 11, 2009 10:57 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 11 guests