列数据1不存在..非法参数例外

时间:2013-06-22 19:03:44

标签: android listview contactscontract

我有这个customListView,显示手机上的所有联系人,每行有一个复选框,供多个选择。问题是我无法显示电话号码。只出现联系人姓名。代码是

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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;



public class AddContacts extends Activity implements OnClickListener {

static final String[] FROM = { ContactsContract.CommonDataKinds.Phone.NUMBER,
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
static final int[] TO = { R.id.contact_name, R.id.phone_number };
ListView list;
Cursor cursor;
SimpleCursorAdapter adapter;
Button AddNumber;    
static final String TAG = "In AddContacts";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    AddNumber = (Button) findViewById(R.id.AddNumbers);
    number1 = (EditText) findViewById(R.id.NumberField1);
    number2 = (EditText) findViewById(R.id.NumberField2);
    number3 = (EditText) findViewById(R.id.NumberField3);
    AddNumber.setOnClickListener(this);
    list = (ListView)findViewById(R.id.lists);
    Log.d(TAG, "onCreate");


        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection = new String[] { ContactsContract.Contacts._ID,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
        String selection = ContactsContract.CommonDataKinds.Phone.IN_VISIBLE_GROUP
                + " = '1'";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                + " COLLATE LOCALIZED ASC";

        cursor = managedQuery(uri, projection, selection, selectionArgs,
                sortOrder);

        adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM, TO);
        list.setAdapter(adapter);
    }

@Override
public void onClick(View v) {
    Log.d(TAG, "onClicked method");
    }
}

现在程序在运行时因IllegalArgument异常而崩溃。当我使用final String[] FROM = { ContactsContract.CommonDataKinds.Phone.NUMBER时,它引起了问题。 logcat是

    06-22 14:48:54.932: E/AndroidRuntime(11073): FATAL EXCEPTION: main
06-22 14:48:54.932: E/AndroidRuntime(11073): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.s/com.example.s.AddContacts}: java.lang.IllegalArgumentException: column 'data1' does not exist
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.os.Looper.loop(Looper.java:137)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.ActivityThread.main(ActivityThread.java:5041)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at java.lang.reflect.Method.invokeNative(Native Method)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at java.lang.reflect.Method.invoke(Method.java:511)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at dalvik.system.NativeStart.main(Native Method)
06-22 14:48:54.932: E/AndroidRuntime(11073): Caused by: java.lang.IllegalArgumentException: column 'data1' does not exist
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:333)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:81)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at com.pratik.safetytrack.AddContacts.onCreate(AddContacts.java:64)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.Activity.performCreate(Activity.java:5104)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-22 14:48:54.932: E/AndroidRuntime(11073):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-22 14:48:54.932: E/AndroidRuntime(11073):    ... 11 more

请告诉我我做错了什么

感谢。

1 个答案:

答案 0 :(得分:0)

   USE CURSOR FOR THIS..FOR E.G


   Cursor phones = 
   getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,
   null);

    pno=new String[phones.getCount()];
   Log.d("rows",Integer.toString(phones.getCount()));

    Log.d("phone","here");
     while (phones.moveToNext()) { 
     String phoneNumber =    

   phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String   
   name=phones.getString(phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Log.i("Number + Name", phoneNumber + name);
   }        

    if(phones.getCount()>0)
      {
       int i=0;
       if(phones.moveToFirst())
        {
           do
            {
      String phoneNumber1 =   
      phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
      String  
      name1=phones.getString(phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
               pno[i]=name1 + "  , " + phoneNumber1;
            i++; 
            }while(phones.moveToNext());
        }

       phones.close();

      }
        ArrayAdapter<String> adapter=new ArrayAdapter<String> 
       (ContactFetch.this,android.R.layout.simple_list_item_1,pno);
       mContactList.setAdapter(adapter);

        }