0

我一直在尝试使用以下代码将联系人导出为 vcf 文件:

b5.setOnClickListener(new 

OnClickListener()
{

public void onClick(View v){
getVCF();
}


public static void getVCF() 

{

final String vfile = "POContactsRestore.vcf";

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

phones.moveToFirst();
for(int i =0;i<phones.getCount();i++)
{
  String lookupKey =    phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
 Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);

AssetFileDescriptor fd;
 try 
 {
     fd = mContext.getContentResolver().openAssetFileDescriptor(uri, "r");
     FileInputStream fis = fd.createInputStream();
     byte[] buf = new byte[(int) fd.getDeclaredLength()];
     fis.read(buf);
     String VCard = new String(buf);
     String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
     FileOutputStream mFileOutputStream = new FileOutputStream(path, true);
                mFileOutputStream.write(VCard.toString().getBytes());           
     phones.moveToNext();                           
     Log.d("Vcard",  VCard);
 } 
 catch (Exception e1) 
 {
      // TODO Auto-generated catch block
      e1.printStackTrace();
 }

 }
 }

问题是这给了我一个“nullpointerexception”,老实说我不太了解调试器。请帮我。

我该如何解决?

编辑1:

我不确定 Logcat 到底应该说什么,但所有错误都如下所示:

com.android.exchange   Strict Mode      null
com.android.exchange   Strict Mode      android.app.ServiceConnectionLeaked: Service com.android exchange

调试窗口突出显示这一行:

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

谢谢你的帮助。

编辑2:

OK调试窗口说:

Thread[<1>main](Suspended (exception NullPointerException))
MainActivity$1.getVCF()line:71
MainActivity$1.onClick(View)line:63

LogCat 窗口只显示我之前发布的错误。

4

1 回答 1

0

(在问题编辑中回答。转换为社区 wiki 答案。见将问题的答案添加到问题本身时的适当操作是什么?

OP写道:

只是为了让你知道我是如何修复它的;我刚刚摆脱了上下文中的“静态”标签:

public class MainActivity extends Activity
{
 static Context mContext;

为了

public class MainActivity extends Activity
{
 Context mContext;

我花了一整天才找出问题所在。

于 2015-02-08T14:33:42.540 回答