J'essaie de choisir le nom d'affichage, le numéro de téléphone mobile des contacts dans le téléphone et de les afficher dans une liste en utilisant une vue de liste. Mais je reçois NullPointerException tout en exécutant le code suivant:Pointeur nul Exception
public class SeeActivity extends ListActivity {
public String [] allname;
public String [] allnumber;
public int listcount=0;
public String [] details;
String[] PROJECTION = new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
;
Cursor phone= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,PROJECTION ,null ,null ,null);
try{
while(phone.moveToNext())
{
String name= phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number=phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
allname[listcount]=name;
allnumber[listcount]=number;
details [listcount]=name+" "+number;
listcount=listcount+1;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,details);
setListAdapter(adapter);
ListView l=getListView();
l.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(),((TextView)arg1).getText() , Toast.LENGTH_SHORT).show();
}
});}
catch(Exception e)
{
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}
}
Où est la Stacktrace? (dans quelle ligne est le NullPointerException jeté?) – MrSmith42
vous voudrez peut-être lire en premier suivant http://developer.android.com/guide/topics/providers/contacts-provider.html#RawContactBasics – David