在我的程序中我试图读取所有联系人并将其转换为.vcf文件。当我运行该应用程序时,联系人将转换为.vcf文件并打印在控制台中并存储在内存中。但我的应用程序不幸停止它会崩溃。请帮助我。我想知道即使我得到输出为什么我的应用程序崩溃...在这里我需要知道为什么我的应用程序不幸停止尽管得到确切的输出
我的代码:
package com.example.ip.newvcf;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Cursor cursor;
ArrayList<String> vCard;
Context mContext;
final String vfile = "lll.vcf";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
// 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();
}
}
}
}
我的错误日志:
09-11 08:23:22.329 25017-25017/? E/dalvikvm: >>>>> Normal User
09-11 08:23:22.329 25017-25017/? E/dalvikvm: >>>>> com.example.ip.newvcf [ userId:0 | appId:10740 ]
09-11 08:23:22.469 25017-25017/com.example.ip.newvcf E/InstantRun: Could not find slices in APK; aborting.
09-11 08:23:22.679 25017-25017/com.example.ip.newvcf E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
09-11 08:23:46.169 25017-25017/com.example.ip.newvcf E/Looper: Error adding epoll events for fd -24, errno=9
09-11 08:23:46.179 25017-25017/com.example.ip.newvcf E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ip.newvcf, PID: 25017
java.lang.RuntimeException: Failed to initialize display event receiver. status=-2147483648
at android.view.DisplayEventReceiver.nativeInit(Native Method)
at android.view.DisplayEventReceiver.<init>(DisplayEventReceiver.java:61)
at android.view.Choreographer$FrameDisplayEventReceiver.<init>(Choreographer.java:747)
at android.view.Choreographer.<init>(Choreographer.java:177)
at android.view.Choreographer.<init>(Choreographer.java:73)
at android.view.Choreographer$1.initialValue(Choreographer.java:99)
at android.view.Choreographer$1.initialValue(Choreographer.java:92)
at java.lang.ThreadLocal$Values.getAfterMiss(ThreadLocal.java:430)
at java.lang.ThreadLocal.get(ThreadLocal.java:65)
at android.view.Choreographer.getInstance(Choreographer.java:202)
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:492)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2505)
at android.app.ActivityThread.access$900(ActivityThread.java:171)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5679)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)