【问题标题】:Null pointer exception while generating vcf file for the contacts [duplicate]为联系人生成 vcf 文件时出现空指针异常 [重复]
【发布时间】:2018-02-08 16:52:33
【问题描述】:
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)

【问题讨论】:

  • 您尚未设置mContext。只需使用getContentResolver().query(...)。为什么还需要持有Context 的静态实例?

标签: java android nullpointerexception contacts


【解决方案1】:

你得到 NPE 是因为你的上下文是空的,或者你可以说你没有初始化上下文。所以需要设置上下文或将引用传递给上下文变量。

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

【讨论】:

  • 我认为首先不需要mContext,因为AppCompatActivity 扩展了Context
【解决方案2】:

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);

【讨论】:

    【解决方案3】:

    由于你在activity里面有方法,你不必使用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();
                }
            }
        }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多