我想製作「聯繫人姓名」的.csv文件和相應的「聯繫電話」。但是當我運行下面的代碼給出錯誤發生pls。看到我的代碼下面,並建議我在哪裏我錯了。 意味着我有兩列之一是名字,第二是聯繫號碼和價值應該是showh下coloumn.so請參閱我的代碼。 我是Android和java新手。關於製作.csv聯繫人列表的問題:android
public class contactlist extends Activity {
static String name;
static int count;
static int countno;
File file = null ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//error on below line
String phoneno = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
cur.close();
String columnString = "\"PersonName\",\"phoneno\"";
String dataString = null;
dataString = "\"" + name +"\",\"" + phoneno + "\"";
Log.i("saurabh",dataString);
String combinedString = columnString + "\n" + dataString;
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()) {
File dir = new File (root.getAbsolutePath() + "/PersonData");
dir.mkdirs();
file = new File(dir, "Data.csv");
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
out.write(combinedString.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
誤差....
07-28 16:09:56.758: ERROR/AndroidRuntime(27597): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
07-28 16:09:56.758: ERROR/AndroidRuntime(27597): at android.database.CursorWindow.getString_native(Native Method)
07-28 16:09:56.758: ERROR/AndroidRuntime(27597): at android.database.CursorWindow.getString(CursorWindow.java:329)
07-28 16:09:56.758: ERROR/AndroidRuntime(27597): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
07-28 16:09:56.758: ERROR/AndroidRuntime(27597): at android.database.CursorWrapper.getString(CursorWrapper.java:135)
07-28 16:09:56.758: ERROR/AndroidRuntime(27597): at contactlist.pkg.contactlist.onCreate(contactlist.java:83)
07-28 16:09:56.758: ERROR/AndroidRuntime(27597): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-28 16:09:56.758: ERROR/AndroidRuntime(27597): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
您的代碼不可讀,請正確格式化! –
我爲他做了。另外@ram ji,你不必使用多個try ... catch塊。 –
先生你可以給我想法如何使csv文件的聯繫信息。所以不矛盾不會發生意味着如果我保存名稱在一個數組和contactno在另一個數組中,並且如果任何聯繫人詳細信息沒有contactno但只有聯繫人名稱然後不一致應該..所以請幫助我先生... –