生成联系人的vcf文件时出现空指针异常

时间:2018-02-08 16:52:33

标签: java android nullpointerexception contacts

public class MainActivity extends AppCompatActivity {

Cursor cursor;
ArrayList<String> vCard;
String vfile;
static Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getVCF();
}

public static void getVCF() {
    final String vfile = "Contacts.vcf";

    Cursor phones = mContext.getContentResolver().query(    // exception here at this line
            ContactsContract.Contacts.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();
        }
    }
}
}

错误

    FATAL EXCEPTION: main  Process: com.example..myapplication, P  
    java.lang.RuntimeException: Unable to start activity 
 ComponentInfo{com.example..myapplication/com.example.myapplication.MainActivity
  }: java.lang.NullPointerException: Attempt to invoke virtual method 
  'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference                                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object referenc   at com.example..myapplication.MainActivity.getVCF(MainActivity.java:36at com.example..myapplication.MainActivity.onCreate(MainActivity.java:30)

3 个答案:

答案 0 :(得分:1)

您获得NPE是因为您的上下文为空,或者您可以说您的非初始化上下文。因此需要设置上下文或将引用传递给上下文变量。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContext = MainActivity.this;
    getVCF();
}

答案 1 :(得分:0)

  

java.lang.NullPointerException:尝试调用虚方法     空对象引用上的'android.content.ContentResolver android.content.Context.getContentResolver()'引起:java.lang.NullPointerException:尝试调用虚方法'android.content.ContentResolver android.content.Context.getContentResolver() '在com.example..myapplication.MainActivity.getVCF上的空对象引用(MainActivity.java:36at com.example..myapplication.MainActivity.onCreate(MainActivity.java:30)

你的异常清楚地告诉mContext在这一行是空的:

Cursor phones = mContext.getContentResolver().query(    // exception here at this line
            ContactsContract.Contacts.CONTENT_URI, null,  
            null, null, null);

在调用getVCF();

之前在onCreate中添加此行
mContext = MainActivity.this;

或者只需将getContentResolver()移除mContext,只需使用它:

getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);

答案 2 :(得分:0)

由于您在活动中使用了该方法,因此您无需使用context.即可直接访问ContentResolver

public static void getVCF() {
        final String vfile = "Contacts.vcf";

        Cursor phones = getContentResolver().query(    // exception here at this line
                ContactsContract.Contacts.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 = 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();
            }
        }
    }
    }