这是我的ContactManager.java
package com.example.android.contactmanager;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public final class ContactManager extends Activity implements OnItemClickListener
{
public static final String TAG = "ContactManager";
private Button mAddAccountButton;
private ListView mContactList;
private boolean mShowInvisible;
//public BooleanObservable ShowInvisible = new BooleanObservable(false);
private CheckBox mShowInvisibleControl;
/**
* Called when the activity is first created. Responsible for initializing the UI.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.v(TAG, "Activity State: onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_manager);
// Obtain handles to UI objects
mAddAccountButton = (Button) findViewById(R.id.addContactButton);
mContactList = (ListView) findViewById(R.id.contactList);
mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible);
// Initialise class properties
mShowInvisible = false;
mShowInvisibleControl.setChecked(mShowInvisible);
// Register handler for UI elements
mAddAccountButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "mAddAccountButton clicked");
launchContactAdder();
}
});
mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
mShowInvisible = isChecked;
populateContactList();
}
});
mContactList = (ListView) findViewById(R.id.contactList);
mContactList.setOnItemClickListener(this);
// Populate the contact list
populateContactList();
}
/**
* Populate the contact list based on account currently selected in the account spinner.
*/
private void populateContactList() {
// Build adapter with contact entries
Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME,
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
mContactList.setAdapter(adapter);
}
/**
* Obtains the contact list for the currently selected account.
*
* @return A cursor for for accessing the contact list.
*/
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
//Log.i("Uri ContactsContract.Contacts.CONTENT_URI" + ContactsContract.Contacts.CONTENT_URI, null, null);
String[] projection = new String[]{
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
//ContactsContract.CommonDataKinds.Email.DATA1
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + (mShowInvisible ? "0" : "1") + "'";
//String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + (mShowInvisible.get() ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return this.managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
/**
* Launches the ContactAdder activity to add a new contact to the selected account.
*/
protected void launchContactAdder()
{
Intent i = new Intent(this, ContactAdder.class);
startActivity(i);
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Log.i("TAG", "You clicked item " + id + " at position " + position);
Cursor cursor = getContacts();
Cursor emailCur = getContactsEmail();
emailCur.isNull(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
cursor.moveToPosition(position);
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// String email = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
//String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
//Log.i("Address", "Testing" + cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SingleListContact.class);
// SingleListContact.PutDetails(ContactsContract.Contacts._ID, name, address, position, null, null);
SingleListContact.PutDetails(ContactsContract.Contacts._ID, name, null);
Log.i("Show Contact Clicked: ", name + "Email: " + null);
startActivity(i);
}
private Cursor getContactsEmail()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
//Log.i("Uri ContactsContract.Contacts.CONTENT_URI" + ContactsContract.Contacts.CONTENT_URI, null, null);
String[] projection = new String[]
{
ContactsContract.Contacts._ID,
ContactsContract.CommonDataKinds.Email.DATA,
// ContactsContract.CommonDataKinds.StructuredPostal.DATA1
//ContactsContract.CommonDataKinds.Phone.DATA
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + (mShowInvisible ? "0" : "1") + "'";
//String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + (mShowInvisible.get() ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.CommonDataKinds.Email.DATA + " COLLATE LOCALIZED ASC";
//
return this.managedQuery(uri, projection, null, selectionArgs, null);
}
}
在添加emailCur之前,一切正常,但在添加代码以获取联系人详细信息后,一切都崩溃了!我可以在这有解决方案吗? Logcat如下:
12-27 22:20:11.129: E/AndroidRuntime(9792): java.lang.IllegalArgumentException: Invalid column data1
12-27 22:20:11.129: E/AndroidRuntime(9792): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:144)
12-27 22:20:11.129: E/AndroidRuntime(9792): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
12-27 22:20:11.129: E/AndroidRuntime(9792): at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330)
12-27 22:20:11.129: E/AndroidRuntime(9792): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
12-27 22:20:11.129: E/AndroidRuntime(9792): at android.content.ContentResolver.query(ContentResolver.java:262)
12-27 22:20:11.129: E/AndroidRuntime(9792): at android.app.Activity.managedQuery(Activity.java:1550)
12-27 22:20:11.129: E/AndroidRuntime(9792): at com.example.android.contactmanager.ContactManager.getContactsEmail(ContactManager.java:193)
12-27 22:20:11.129: E/AndroidRuntime(9792): at com.example.android.contactmanager.ContactManager.onItemClick(ContactManager.java:153)
12-27 22:20:11.129: E/AndroidRuntime(9792): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-27 22:20:11.129: E/AndroidRuntime(9792): at android.widget.ListView.performItemClick(ListView.java:3749)
12-27 22:20:11.129: E/AndroidRuntime(9792): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1986)
12-27 22:20:11.129: E/AndroidRuntime(9792): at android.os.Handler.handleCallback(Handler.java:587)
12-27 22:20:11.129: E/AndroidRuntime(9792): at android.os.Handler.dispatchMessage(Handler.java:92)
12-27 22:20:11.129: E/AndroidRuntime(9792): at android.os.Looper.loop(Looper.java:130)
12-27 22:20:11.129: E/AndroidRuntime(9792): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-27 22:20:11.129: E/AndroidRuntime(9792): at java.lang.reflect.Method.invokeNative(Native Method)
12-27 22:20:11.129: E/AndroidRuntime(9792): at java.lang.reflect.Method.invoke(Method.java:507)
12-27 22:20:11.129: E/AndroidRuntime(9792): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-27 22:20:11.129: E/AndroidRuntime(9792): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-27 22:20:11.129: E/AndroidRuntime(9792): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
在从中访问值之前,为emailCur添加空检查。