联系Picker应用程序在手机上崩溃但在模拟器上没有崩溃

时间:2013-07-31 10:48:21

标签: android spinner logcat

所以我有来自Android的关于Spinners的这个示例代码,我修改了一下以显示其中一个微调器中的联系人,并且在模拟器上它工作正常,它显示了我在联系人中添加的5个条目,但在手机上它在启动时崩溃,我无法理解LogCat的任何内容,可能是什么问题? 这里是代码

// add items into spinner dynamically
    public void addItemsOnSpinner2() {
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        list = new ArrayList<String>();
        getContacts(null);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(dataAdapter);
    }

    private String getContacts(String contact) {

        people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (people.moveToNext()) {
            int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
            contact = people.getString(nameFieldColumnIndex);
            list.add(contact);
        }
        people.close();
        return contact;
    }

,这是在LogCat

07-31 13:35:55.638: I/Process(11956): Sending signal. PID: 11956 SIG: 9
07-31 13:36:06.990: I/Process(12013): Sending signal. PID: 12013 SIG: 9
07-31 13:38:02.463: E/Trace(12130): error opening trace file: No such file or directory (2)
07-31 13:38:02.463: D/ActivityThread(12130): setTargetHeapUtilization:0.25
07-31 13:38:02.463: D/ActivityThread(12130): setTargetHeapIdealFree:8388608
07-31 13:38:02.473: D/ActivityThread(12130): setTargetHeapConcurrentStart:2097152
07-31 13:38:02.584: D/AndroidRuntime(12130): Shutting down VM
07-31 13:38:02.584: W/dalvikvm(12130): threadid=1: thread exiting with uncaught exception (group=0x40d7b450)
07-31 13:38:02.594: E/AndroidRuntime(12130): FATAL EXCEPTION: main
07-31 13:38:02.594: E/AndroidRuntime(12130): java.lang.NullPointerException
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.widget.Spinner.onMeasure(Spinner.java:440)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.View.measure(View.java:15348)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1420)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.View.measure(View.java:15348)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.View.measure(View.java:15348)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:849)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.View.measure(View.java:15348)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2154)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.View.measure(View.java:15348)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1860)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1108)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1281)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1006)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4225)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.Choreographer.doFrame(Choreographer.java:525)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.os.Handler.handleCallback(Handler.java:615)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.os.Looper.loop(Looper.java:213)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at android.app.ActivityThread.main(ActivityThread.java:4787)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at java.lang.reflect.Method.invokeNative(Native Method)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at java.lang.reflect.Method.invoke(Method.java:511)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
07-31 13:38:02.594: E/AndroidRuntime(12130):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

结果我手机中的第一个条目是空的(null),谁知道为什么......我修好了这个

public void getContacts() {

        people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (people.moveToNext()) {
            int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
            String s = people.getString(nameFieldColumnIndex);
            if (s != null)
                list.add(s);
        }
        people.close();
    }