新联系人 class 和错误 - 应用已停止 - Android

New contacts class and error - app was stopped - Android

我在 Android 项目中创建了一个新的 class,它正在读取 phone 书中的联系人。这个class是:

public class KontaktBook extends Activity {
    public void displayContacts() {
        Toast.makeText(KontaktBook.this, "Name:", Toast.LENGTH_SHORT).show();
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(cur.getString(
                        cur.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()) {
                        String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Toast.makeText(KontaktBook.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                    }
                    pCur.close();
                }
            }
        }
    }
}

在主要 class 中,我创建了新的 KontaktBook 对象,如下所示:

    KontaktBook kont = new KontaktBook();
    kont.displayContacts(); 

当我把这个程序放入 Android phone 并引入 kont.displayContacts();

我在 phone 中收到错误:"Unfortunately,aplication was stopped"。

哪里会出问题?

当我将此片段(在方法 displayContacts() 中)放入 Main Class (MainActivity) 时,问题消失了 - 没问题。

PS Logcat 显示:

08-10 13:32:15.080  11584-11584/com.example.proj E/dalvikvm﹕ could not disable core file generation for pid 11584: Operation not permitted
08-10 13:32:15.200  11584-11584/com.example.proj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
08-10 13:32:15.200  11584-11584/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 12214: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
08-10 13:32:15.200  11584-11584/com.example.proj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
08-10 13:32:15.200  11584-11584/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 12220: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
08-10 13:32:15.200  11584-11584/com.example.proj I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
08-10 13:32:15.200  11584-11584/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 9785: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
08-10 13:32:15.210  11584-11584/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
08-10 13:32:15.210  11584-11584/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 392: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
08-10 13:32:15.210  11584-11584/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
08-10 13:32:15.210  11584-11584/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 414: Landroid/content/res/TypedArray;.getType (I)I
08-10 13:32:15.210  11584-11584/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
08-10 13:32:15.210  11584-11584/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 355: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-10 13:32:15.220  11584-11584/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
08-10 13:32:15.220  11584-11584/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 357: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-10 13:32:15.350  11584-11584/com.example.proj W/linker﹕ library "libmaliinstr.so" not found
08-10 13:32:15.350  11584-11584/com.example.proj W/linker﹕ error:
08-10 13:32:42.140  12032-12032/com.example.proj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
08-10 13:32:42.140  12032-12032/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 12214: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
08-10 13:32:42.140  12032-12032/com.example.proj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
08-10 13:32:42.140  12032-12032/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 12220: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
08-10 13:32:42.150  12032-12032/com.example.proj I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
08-10 13:32:42.150  12032-12032/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 9785: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
08-10 13:32:42.160  12032-12032/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
08-10 13:32:42.160  12032-12032/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 392: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
08-10 13:32:42.160  12032-12032/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
08-10 13:32:42.160  12032-12032/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 414: Landroid/content/res/TypedArray;.getType (I)I
08-10 13:32:42.160  12032-12032/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
08-10 13:32:42.170  12032-12032/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 355: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-10 13:32:42.170  12032-12032/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
08-10 13:32:42.170  12032-12032/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 357: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-10 13:32:42.330  12032-12032/com.example.proj W/linker﹕ library "libmaliinstr.so" not found
08-10 13:32:42.330  12032-12032/com.example.proj W/linker﹕ error:
08-10 13:32:42.990  12032-12032/com.example.proj W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41774c50)
08-10 13:39:02.080  12396-12396/com.example.proj E/dalvikvm﹕ could not disable core file generation for pid 12396: Operation not permitted
08-10 13:39:02.210  12396-12396/com.example.proj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
08-10 13:39:02.210  12396-12396/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 12214: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
08-10 13:39:02.210  12396-12396/com.example.proj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
08-10 13:39:02.210  12396-12396/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 12220: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
08-10 13:39:02.220  12396-12396/com.example.proj I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
08-10 13:39:02.220  12396-12396/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 9785: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
08-10 13:39:02.230  12396-12396/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
08-10 13:39:02.230  12396-12396/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 392: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
08-10 13:39:02.230  12396-12396/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
08-10 13:39:02.230  12396-12396/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 414: Landroid/content/res/TypedArray;.getType (I)I
08-10 13:39:02.250  12396-12396/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
08-10 13:39:02.250  12396-12396/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 355: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-10 13:39:02.250  12396-12396/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
08-10 13:39:02.250  12396-12396/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 357: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-10 13:39:02.450  12396-12396/com.example.proj W/linker﹕ library "libmaliinstr.so" not found
08-10 13:39:02.450  12396-12396/com.example.proj W/linker﹕ error:
08-10 13:39:22.320  12620-12620/com.example.proj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
08-10 13:39:22.320  12620-12620/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 12214: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
08-10 13:39:22.320  12620-12620/com.example.proj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
08-10 13:39:22.330  12620-12620/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 12220: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
08-10 13:39:22.330  12620-12620/com.example.proj I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
08-10 13:39:22.330  12620-12620/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 9785: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
08-10 13:39:22.340  12620-12620/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
08-10 13:39:22.340  12620-12620/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 392: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
08-10 13:39:22.340  12620-12620/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
08-10 13:39:22.340  12620-12620/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 414: Landroid/content/res/TypedArray;.getType (I)I
08-10 13:39:22.350  12620-12620/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
08-10 13:39:22.350  12620-12620/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 355: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-10 13:39:22.350  12620-12620/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
08-10 13:39:22.350  12620-12620/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 357: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-10 13:39:22.500  12620-12620/com.example.proj W/linker﹕ library "libmaliinstr.so" not found
08-10 13:39:22.500  12620-12620/com.example.proj W/linker﹕ error:
08-10 13:39:23.830  12620-12620/com.example.proj W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41774c50)
08-10 13:39:25.670  12620-12620/com.example.proj I/Process﹕ Sending signal. PID: 12620 SIG: 9
08-10 13:40:00.660  12848-12848/com.example.proj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
08-10 13:40:00.660  12848-12848/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 12214: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
08-10 13:40:00.660  12848-12848/com.example.proj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
08-10 13:40:00.660  12848-12848/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 12220: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
08-10 13:40:00.670  12848-12848/com.example.proj I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
08-10 13:40:00.670  12848-12848/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 9785: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
08-10 13:40:00.680  12848-12848/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
08-10 13:40:00.680  12848-12848/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 392: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
08-10 13:40:00.680  12848-12848/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
08-10 13:40:00.680  12848-12848/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 414: Landroid/content/res/TypedArray;.getType (I)I
08-10 13:40:00.690  12848-12848/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
08-10 13:40:00.690  12848-12848/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 355: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-10 13:40:00.690  12848-12848/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
08-10 13:40:00.690  12848-12848/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 357: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-10 13:40:00.800  12848-12848/com.example.proj W/linker﹕ library "libmaliinstr.so" not found
08-10 13:40:00.800  12848-12848/com.example.proj W/linker﹕ error:
08-10 13:40:02.170  12848-12848/com.example.proj W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41774c50)
08-10 13:52:03.510  13264-13264/com.example.proj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
08-10 13:52:03.510  13264-13264/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 12214: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
08-10 13:52:03.510  13264-13264/com.example.proj I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
08-10 13:52:03.510  13264-13264/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 12220: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
08-10 13:52:03.510  13264-13264/com.example.proj I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
08-10 13:52:03.510  13264-13264/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 9785: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
08-10 13:52:03.530  13264-13264/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
08-10 13:52:03.530  13264-13264/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 392: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
08-10 13:52:03.530  13264-13264/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
08-10 13:52:03.530  13264-13264/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 414: Landroid/content/res/TypedArray;.getType (I)I
08-10 13:52:03.540  13264-13264/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
08-10 13:52:03.540  13264-13264/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 355: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-10 13:52:03.540  13264-13264/com.example.proj I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
08-10 13:52:03.540  13264-13264/com.example.proj W/dalvikvm﹕ VFY: unable to resolve virtual method 357: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
08-10 13:52:03.750  13264-13264/com.example.proj W/linker﹕ library "libmaliinstr.so" not found
08-10 13:52:03.750  13264-13264/com.example.proj W/linker﹕ error:
08-10 13:52:16.480  13264-13264/com.example.proj W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41774c50)
08-10 13:57:16.560  13264-13264/com.example.proj I/Process﹕ Sending signal. PID: 13264 SIG: 9

您必须使用 Intent 来启动 KontaktBook activity,并且您可以在 KontaktBook Activity 的 Create 方法中调用 display Contacts 方法,因为您已将其声明为 Activity。从 KontaktBook class 中删除扩展 Activity 以直接从 Main Activity.

调用显示联系人方法