列数据 1 不存在.. 非法参数异常
Posted
技术标签:
【中文标题】列数据 1 不存在.. 非法参数异常【英文标题】:Column Data 1 doesnt Exist.. Illegal Argument Exception 【发布时间】:2013-06-19 17:03:45 【问题描述】:我有这个 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 ComponentInfocom.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]。这就是你要找的,我猜。 [1]:***.com/a/12227066/828728 @yugidroid - 这不是我正在做的事情吗?你能进一步解释你的观点吗 【参考方案1】: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);
【讨论】:
但我已经在代码中使用了游标“cursor = managedQuery(uri, projection, selection, selectionArgs,sortOrder);" 您需要使用 while 循环将每个联系人添加到数组中,如图所示,然后使用适配器将其显示在列表视图中.. 我会试一试.. 但我很困惑,如果 listView 显示联系人姓名而没有 while 循环,那么为什么光标需要一个联系人号码循环? while 循环的作用是逐个获取每个联系人并将其存储为一个数组,然后适配器使用该数组并填充一个列表视图..它对我有用.. 我注意到有一个订单项。我正在使用一个包含三个行项目的自定义 listView,所以我需要 FROM 和 TO 字段,这让我再次回到 sqaure 一个以上是关于列数据 1 不存在.. 非法参数异常的主要内容,如果未能解决你的问题,请参考以下文章