获取运行时异常无效列 data2?
我正在尝试获取联系人姓名及其类型,但在代码中标记的行处获取此异常。我在添加类型之前获取了名称,但现在获取了此异常。请帮助。提前致谢。
package application.test;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,Phone.TYPE};
ContentResolver cr = getContentResolver();
ContentResolver ncr=getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection,null, null, Contacts.DISPLAY_NAME + " ASC");
Cursor ncur=ncr.query(ContactsContract.Data.CONTENT_URI, projection, null, null,Contacts.DISPLAY_NAME + " ASC");
Cursor icur = cr.query(ContactsContract.RawContacts.CONTENT_URI, projection,null, null, Contacts.DISPLAY_NAME + " ASC");
if (cur.getCount() >0 && ncur.getCount()>0)
{
while (cur.moveToNext()&& ncur.moveToNext())
{
String id = icur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String type=ncur.getString(cur.getColumnIndex(Phone.TYPE)) ;
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
Cursor typecur = ncr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
while (pCur.moveToNext()&& typecur.moveToNext())
{
Log.d("names",name);
Log.d("types",type);
pCur.close();
}
}
}
}
}
}
I am trying to get the contacts name and their types but getting this exception at line marked in the code.I was getting the names prior to adding the types,but now getting this exception.please help.thanks in advance.
package application.test;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,Phone.TYPE};
ContentResolver cr = getContentResolver();
ContentResolver ncr=getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection,null, null, Contacts.DISPLAY_NAME + " ASC");
Cursor ncur=ncr.query(ContactsContract.Data.CONTENT_URI, projection, null, null,Contacts.DISPLAY_NAME + " ASC");
Cursor icur = cr.query(ContactsContract.RawContacts.CONTENT_URI, projection,null, null, Contacts.DISPLAY_NAME + " ASC");
if (cur.getCount() >0 && ncur.getCount()>0)
{
while (cur.moveToNext()&& ncur.moveToNext())
{
String id = icur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String type=ncur.getString(cur.getColumnIndex(Phone.TYPE)) ;
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
Cursor typecur = ncr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
while (pCur.moveToNext()&& typecur.moveToNext())
{
Log.d("names",name);
Log.d("types",type);
pCur.close();
}
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在投影中,Phone.Type 不是您正在查询的联系人表的一部分。
详细解释一下你正在尝试什么。
如果您正在查看手机类型,那么您应该查询数据表
注意:我假设您使用的是 Android 版本 2.0 +,而不是 1.5 或 1.6
In projection, Phone.Type is not a part of Contacts Table which you are querying.
Explain in detail, what you are trying.
If you are looking at Type of Phone, then you should be querying Data table
NOTE: I presume that you are using Android version 2.0 + and not 1.5 or 1.6