Logcat 说“无效的列 data1”
Posted
技术标签:
【中文标题】Logcat 说“无效的列 data1”【英文标题】:Logcat says "invalid column data1" 【发布时间】:2013-06-19 18:38:41 【问题描述】:线程新手。 SQL 新手。获取联系信息的新手。所以我当然迷路了。如果我正确阅读了这个 logcat,它会告诉我:列 data1 不存在,或者我正在寻找错误的信息。不幸的是,这是一条“从错误中学习”的学习路径,我无法弄清楚这一点。非常感谢任何帮助。
这里的目标是获取联系人的姓名、电话号码和电子邮件(通过联系人 ID 匹配信息)。
日志:
06-22 21:15:44.700: E/androidRuntime(1662): FATAL EXCEPTION: Thread-120
06-22 21:15:44.700: E/AndroidRuntime(1662): java.lang.IllegalArgumentException: Invalid column data1
06-22 21:15:44.700: E/AndroidRuntime(1662): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
06-22 21:15:44.700: E/AndroidRuntime(1662): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
06-22 21:15:44.700: E/AndroidRuntime(1662): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
06-22 21:15:44.700: E/AndroidRuntime(1662): at android.content.ContentResolver.query(ContentResolver.java:372)
06-22 21:15:44.700: E/AndroidRuntime(1662): at android.content.ContentResolver.query(ContentResolver.java:315)
06-22 21:15:44.700: E/AndroidRuntime(1662): at sat.tuts4mobile.customlistview.ContactDetails$1.run(ContactDetails.java:53)
06-22 21:15:44.700: E/AndroidRuntime(1662): at java.lang.Thread.run(Thread.java:856)
代码(它正在提取联系人 ID 0 的信息):
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;
/**
* Created by Pete on 6/19/13.
*/
public class ContactDetails extends Activity
TextView tvContactName, tvPhoneNum, tvPhoneType, tvPhoneFull,
tvEmailAdd, tvEmailType, tvEmailFull,
tvAddress, tvAddType, tvAddFull;
String contactId, contactName, phoneType, phoneFull, phoneNum1,
emailAdd, emailType, emailFull,
address, addType, addFull;
//Contact List query arguments
Uri uri;
String[] projection, selectionArgs;
String selection, sortOrder;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.contactinfo);
initialize();
contactId = getIntent().getStringExtra("contactId");
contactName = getIntent().getStringExtra("contactName");
new Thread(new Runnable()
@Override
public void run()
uri = ContactsContract.Contacts.CONTENT_URI;
projection = new String[]
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
;
selection = ContactsContract.Data.CONTACT_ID +
" = " + contactId + " AND " +
ContactsContract.Data.MIMETYPE + " = '" +
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'";
selectionArgs = null;
sortOrder = null;
// Create cursor searching for data associated with contactId
if (contactId != null)
// Return all the PHONE data for the contact
Cursor cursor = getContentResolver().query(
uri, projection, selection, selectionArgs, sortOrder);
//Get the indexes of the required columns
while (cursor.moveToNext())
// Extract the name
contactName = cursor.getString(
cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
tvContactName.setText(contactName);
// Extract the phone number
phoneFull = cursor.getString(
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
tvPhoneFull.post(new Runnable()
@Override
public void run()
// TODO Auto-generated method stub
tvPhoneFull.setText(phoneFull);
);
cursor.close();
).start();
new Thread(new Runnable()
@Override
public void run()
uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
projection = null;
selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID +
" = " + contactId + " AND " +
ContactsContract.Data.MIMETYPE + " = '" +
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE + "'";
selectionArgs = null;
sortOrder = null;
Cursor emailCursor = getContentResolver().query(
uri, projection, selection, selectionArgs, sortOrder);
while (emailCursor.moveToNext())
// Extract email address
emailFull = emailCursor.getString(
emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
tvPhoneFull.post(new Runnable()
@Override
public void run()
tvEmailFull.setText(emailFull);
);
emailCursor.close();
).start();
public void initialize()
tvContactName = (TextView)findViewById(R.id.tvContactName);
tvPhoneNum = (TextView)findViewById(R.id.tvPhoneNum);
tvPhoneType = (TextView)findViewById(R.id.tvPhoneType);
tvPhoneFull = (TextView)findViewById(R.id.tvPhoneFull);
tvEmailAdd = (TextView)findViewById(R.id.tvEmailAdd);
tvEmailType = (TextView)findViewById(R.id.tvEmailType);
tvEmailFull = (TextView)findViewById(R.id.tvEmailFull);
tvAddress = (TextView)findViewById(R.id.tvAddress);
tvAddType = (TextView)findViewById(R.id.tvAddType);
tvAddFull = (TextView)findViewById(R.id.tvAddFull);
【问题讨论】:
您可能希望解释一下这段代码试图完成什么。此外,一旦您解决了这个问题,您将在tvContactName.setText(contactName);
上崩溃,因为您正在后台线程中执行此工作。考虑使用CursorLoader
,或者AsyncTask
,因为两者都提供了更好的线程和UI更新方法。
感谢您的关注。立即编辑 OP
【参考方案1】:
要检索电话号码,请查询 Phone.CONTENT_URI
并将 Phone.NUMBER
包含在您的投影中。
要检索电子邮件地址,请查询 Email.CONTENT_URI
并在您的投影中请求 Email.DATA
。
其中任何一个还允许您在投影中包含Contacts.DISPLAY_NAME
,因为像这样的一些常见列会自动加入。
虽然我之前没有按 ID 检索过这些数据,但我相信您的“where 子句”将分别为 Phone.CONTACT_ID + " = " + contactId
和 Email.CONTACT_ID + " = " + contactId
。请参阅How to get contacts' phone number in Android 了解更多信息。
【讨论】:
更改了选择以匹配你所说的,仍然得到同样的错误 @WizardKnight:不知道该告诉你什么,抱歉。 不用担心。你实际上帮了很多忙。我怀疑一些仔细检查会在某个地方发现一个错字。再次感谢 很高兴知道如何使用developer.android.com/reference/android/provider/…进行投影【参考方案2】:用ContactsContract.PhoneLookup.NORMALIZED_NUMBER
替换ContactsContract.CommonDataKinds.Phone.NUMBER
可能会有所帮助。
Logcat 说列名“data1”无效。
【讨论】:
以上是关于Logcat 说“无效的列 data1”的主要内容,如果未能解决你的问题,请参考以下文章