类不工作,错误发生?

时间:2012-07-05 08:41:08

标签: android dropbox

我正在使用Android 2.1平台。要做一个基于DropBox的项目。我使用DBUtility类来列出DropBox中的联系人。 DBUtility不是主类。它是一个子类.Main类从这个类中调用一些函数。

它运作良好,下面代码中的Createfolder()只包含一个语句

mDBApi.createFolder( “联系人”);

如上所述没有错误。当我添加一些基于联系人的代码时。我得到了错误,如

“方法getContentResolver()未定义类型DropBoxUtility”

代码

public class DropBoxUtility {
    private DropboxAPI<AndroidAuthSession> mDBApi;  
    public DropBoxUtility(DropboxAPI<AndroidAuthSession> Api) {
        mDBApi = Api;  
    } 

    public void ContactFolder() throws DropboxException { 
        mDBApi.createFolder("Contacts");
        ArrayAdapter<String>itemAdapter;
        ArrayList<String> contactlist = new ArrayList<String>(); 
        String displayName="All Contacts Here";
        ByteArrayInputStream inputStream = new ByteArrayInputStream(displayName.getBytes());
        Entry newEntry = mDBApi.putFile("Contacts/abhi.doc", inputStream, displayName.length(), null, null);    

        /* I got Error on this line */
        ContentResolver cr = getContentResolver() ;
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        while (cursor.moveToNext()) {
            displayName = ""; 
            displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String holder = "";
            holder = holder + displayName;

            /* Phone Number and Type */
            ArrayList<String> phoneList = new ArrayList<String>();
            if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);

            while (pCur.moveToNext()) {

                int phoneType = pCur
                        .getInt(pCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                String pnum = pCur.getString(pCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                switch (phoneType) {
                case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                    phoneList.add(", Home, "+pnum);
                    break;
                case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                    phoneList.add(", Work, "+pnum);
                    break;
                case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                    phoneList.add(", Mobile, "+pnum);
                    break;
                case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
                    phoneList.add(", Other, "+pnum);
                    break;
                }
            }   // end of while

            pCur.close(); 
        }       // end of if

        /* Email */
        Cursor emails = cr.query(Email.CONTENT_URI, null, Email.CONTACT_ID
                + " = " + id, null, null);

        ArrayList<String> emailList = new ArrayList<String>();

        while (emails.moveToNext()) {
            int emailType = emails
                    .getInt(emails
                            .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
            String email = emails.getString(emails
                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

            switch (emailType) {
            case ContactsContract.CommonDataKinds.Email.TYPE_HOME:
                emailList.add(", Home, "+email);
                break;
            case ContactsContract.CommonDataKinds.Email.TYPE_WORK:
                emailList.add(", Work, "+email);
                break;
            case ContactsContract.CommonDataKinds.Email.TYPE_MOBILE:
                emailList.add(", Mobile, "+email);
                break;
            case ContactsContract.CommonDataKinds.Email.TYPE_OTHER:
                emailList.add(", Other, "+email);
                break;
            }
        }
        emails.close();


        if(phoneList.size()>=emailList.size()) {
            for(int i=0;i<phoneList.size();i++) {
                holder = displayName + phoneList.get(i);
                try{
                    holder = holder + emailList.get(i);
                }catch(Exception ex){
                    holder = holder + ", , ";
                }
                contactlist.add(holder);
            }
        }else {
            for(int i=0;i<emailList.size();i++) {
                try{
                    holder = displayName+ phoneList.get(i);
                }catch(Exception ex){
                    holder = holder + ", , ";
                }
                holder = holder + emailList.get(i);
                contactlist.add(holder);
            }
        }
    }
    Collections.sort(contactlist);
    cursor.close(); 

}  

logcat的

 07-05 14:57:54.734: W/dalvikvm(392): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
 07-05 14:57:54.734: E/AndroidRuntime(392): Uncaught handler: thread main exiting due to uncaught exception
 07-05 14:57:54.744: E/AndroidRuntime(392): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=392, uid=10028 requires android.permission.READ_CONTACTS
 07-05 14:57:54.744: E/AndroidRuntime(392):     at android.os.Parcel.readException(Parcel.java:1218)
 07-05 14:57:54.744: E/AndroidRuntime(392):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
 07-05 14:57:54.744: E/AndroidRuntime(392):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
 07-05 14:57:54.744: E/AndroidRuntime(392):     at android.content.ContentProviderProxy.bulkQuery(ContentProviderNative.java:326)
 07-05 14:57:54.744: E/AndroidRuntime(392):     at android.content.ContentProviderProxy.query(ContentProviderNative.java:345)
 07-05 14:57:54.744: E/AndroidRuntime(392):     at android.content.ContentResolver.query(ContentResolver.java:202)
 07-05 14:57:54.744: E/AndroidRuntime(392):     at com.abhi.DropBoxUtility.ContactFolder(DropBoxUtility.java:47)

如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

getContentResolver类中存在

Context方法。您可以直接从活动中调用它,因为Activity是一个Context。您的DropBoxUtility不是Activity,因此您需要在其中包含上下文才能使用getContentResolver方法。最简单的方法是将它传递给构造函数:

public class DropBoxUtility
{
    private DropboxAPI<AndroidAuthSession> mDBApi;
    private Context context;

    public DropBoxUtility(DropboxAPI<AndroidAuthSession> Api, Context ctx)
    {
        mDBApi = Api;
        context = ctx;
    }

    ...
}

然后,当您需要拨打电话时,请使用context.getContentResolver()

您可以将活动中的此类实例化为new DropBoxUtility(api, this);

编辑:要阅读联系信息,您需要为清单添加相应的权限:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

答案 1 :(得分:0)

// I got Error on this line     ContentResolver cr = getContentResolver() ;

我不知道 DropBox api ,但我肯定可以指出这个错误,getContentResolver()是android 活动类的方法而不是< strong> DropBoxUtility ,您可以使用此活动的上下文(参考)在不同的类中使用此方法。

将Android Activity的上下文传递给DropBoxUtility构造函数并使用该上下文 等,

ContentResolver cr =  mContext.getContentResolver(); // Here mContext is a reference of your Activity class from which your are calling this DropBox class. 

试试这个,

答案 2 :(得分:0)

DropBoxUtility不会从任何提供getContentResolver方法的类(即Activity)继承。要么必须从另一个类派生类,要么必须声明并实现getContentResolver方法。

正如user370305所说,您可以将Activity的实例传递给DropBoxUtility类的构造函数,并在该实例上调用该方法。