请有人告诉我为什么会出现这个错误?...我已经检查并重新检查了我的代码,一切似乎都没问题。但我不知道为什么当我运行它时,它给了我这个错误 java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序 这是我的java代码
public class Sync extends ListActivity implements OnClickListener {
// Progress Dialog
private ProgressDialog pDialog;
//cursor loader
SimpleCursorAdapter mAdapter;
List<HashMap<String, String>> contactList;
private static final String DisplayName="";
private static final String Given="";
private static final String Family="";
private static final String NickName="";
private static final String HomePhone="";
private static final String MobilePhone="";
private static final String WorkPhone="";
private static final String HomeEmail="";
private static final String WorkEmail="";
private static final String CompanyName="";
private static final String Title="";
ArrayList<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
//ContactHoldObject co;
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
//ListView contactView = (ListView) findViewById(R.id.list);
contactList = new ArrayList<HashMap<String, String>>();
registerForContextMenu(getListView());
// Loading products in Background Thread
new LoadAllContacts().execute();
}
class LoadAllContacts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Sync.this);
pDialog.setMessage("Loading list of training centers. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All contacts
* */
protected String doInBackground(String... args) {
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
// Querying the table ContactsContract.Contacts to retrieve all the contacts
Cursor contactsCursor = getContentResolver().query(contactsUri, null, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
// ContactHoldObject.contactinfo =new ContactInfo[contactsCursor.getCount()];
//co=new ContactHoldObject();
try{
if(contactsCursor.moveToFirst()){
do{
long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));
Uri dataUri = ContactsContract.Data.CONTENT_URI;
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Cursor dataCursor = getContentResolver().query(dataUri, null,
ContactsContract.Data.CONTACT_ID + "=" + contactId,
null, null);
String displayName="";
String given="";
String family="";
String nickName="";
String homePhone="";
String mobilePhone="";
String workPhone="";
String homeEmail="";
String workEmail="";
String companyName="";
String title="";
displayName=dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//contactView.
map.put(DisplayName, displayName);
if(dataCursor.moveToFirst()){
String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = " + contactId;
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
if(nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)) != null
&& !nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)).equals("")){
given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
}else{
given="No firstname";
}
map.put(Given, given);
/*given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
String string = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DATA13));*/
/*System.out.println("Name: " + given + " Family: " + family + " Displayname: " + display + "MIDDLE_NAME"+MIDDLE_NAME+"PHONETIC_FAMILY_NAME"+PHONETIC_FAMILY_NAME+" PHONETIC_MIDDLE_NAME: "+PHONETIC_MIDDLE_NAME+"PREFIX :"+PREFIX+ "SUFFIX :" +SUFFIX+"DATA1:: "+DATA1+ "DATA1 :"+DATA1);*/
}nameCur.close();
do{
// Getting NickName
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE))
nickName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
//System.out.println("nickName "+nickName);
// Getting Phone numbers
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
}
}
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE ) ) {
switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
case ContactsContract.CommonDataKinds.Email.TYPE_HOME :
homeEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Email.TYPE_WORK :
workEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
}
}
// Getting Organization details
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)){
companyName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
title = dataCursor.getString(dataCursor.getColumnIndex("data4"));
}
}while(dataCursor.moveToNext());
String details = "";
String email=" ";
// Concatenating various information to single s
if(homePhone != null && !homePhone.equals("") )
details = "HomePhone : " + homePhone + "\n";
if(mobilePhone != null && !mobilePhone.equals("") )
details += "MobilePhone : " + mobilePhone + "\n";
if(workPhone != null && !workPhone.equals("") )
details += "WorkPhone : " + workPhone + "\n";
if(nickName != null && !nickName.equals("") )
details += "NickName : " + nickName + "\n";
if(homeEmail != null && !homeEmail.equals("") ){
details += homeEmail + "\n";
email=homeEmail;
}
if(workEmail != null && !workEmail.equals("") ){
details += workEmail + "\n";
email=workEmail;
}
if(companyName != null && !companyName.equals("") )
details += "CompanyName : " + companyName + "\n";
if(title != null && !title.equals("") )
details += "Title : " + title + "\n";
if(details!= null && !details.equals("") ){
// System.out.println(given+"\n"+details);
// adding each child node to HashMap key => value
map.put(Family, family);
map.put(NickName, nickName);
map.put(HomePhone, homePhone);
map.put(WorkPhone, workPhone);
map.put(HomeEmail, homeEmail);
map.put(MobilePhone, mobilePhone);
map.put(WorkEmail, workEmail);
map.put(CompanyName, companyName);
map.put(Title, title);
// adding HashList to ArrayList
contactList.add(map);
list = new ArrayList<String>(map.values());
//addContacts(given,email);
}
}dataCursor.close();
}while(contactsCursor.moveToNext());
}contactsCursor.close();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText( getBaseContext(), "can't get it", Toast.LENGTH_LONG).show();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
Sync.this, contactList,
R.layout.trackrow, new String[] {
DisplayName},
new int[] { R.id.textName });
// updating listview
setListAdapter(adapter);
}
});
}
//return "success";
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Map<String,String> item = (Map<String, String>) l.getItemAtPosition(position);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
你正在尝试在非UI线程(doInBackGround
)上执行UI操作。
Toast.makeText( getBaseContext(), "can't get it", Toast.LENGTH_LONG).show();
尝试在doInBackGround
中删除此吐司,或只使用runonUithead
显示toast
。
且onPostExecute
已在UI线程中运行,无需使用runonUiThread