PhoneGap-联系人没有显示

时间:2014-04-02 10:15:49

标签: android cordova phonegap-build

我正在尝试使用PhoneGap开发来显示设备中的联系人列表。但它没有显示联系人。

它既没有出现任何错误。

请任何人帮助我找出以下代码中的错误,因为我在phonegap中是begginer。

  <!DOCTYPE HTML>
<html>
<head>
<title> Contacts Demo </title>
<script type="text/javascript" src="phonegap-1.1.0.js"></script>
<script type="text/javascript">

    function init()
    {
        document.addEventListener("deviceready",onDeviceReady,false);
    }

    function onDeviceReady()
    {
        var options=new ContactFindOptions();
        options.filter="";
        var fields=["name","phoneNumbers"];
        navigator.service.contacts.find(fields,onSuccess,onError,options);
    }

    function onSuccess(contacts)
    {
        var ulr=document.getElementById("list");
        for(var i=0;i<contacts.length;i++)
        {
            var name=contacts[i].name.formatted;
            var phoneNumber=contacts[i].phoneNumbers[0].value;
            var li=document.createElement('li');
            li.innerHTML = "<a href=\"tel://"+phoneNumber+"\">"+name+"</a>";
            ulr=appendChild(li);
        }
    };

    function onError()
    {
        alert('onError!');
    };
</script>
</head>
<body>
<h1>Contacts List</h1>
<ul id="list"></ul>
</body>
</html>

logcat的:

04-02 16:05:37.167: I/PhoneGapLog(7141): phonegap.xml missing. Ignoring...
04-02 16:05:37.167: I/System.out(7141): loadUrl(file:///android_asset/www/contactindex.html)
04-02 16:05:37.167: I/System.out(7141): url=file:///android_asset/www/contactindex.html baseUrl=file:///android_asset/www/
04-02 16:05:37.377: D/SoftKeyboardDetect(7141): We are in our onMeasure method
04-02 16:05:37.377: D/SoftKeyboardDetect(7141): Old Height = 0
04-02 16:05:37.377: D/SoftKeyboardDetect(7141): Height = 762
04-02 16:05:37.377: D/SoftKeyboardDetect(7141): Old Width = 0
04-02 16:05:37.377: D/SoftKeyboardDetect(7141): Width = 480
04-02 16:05:37.377: D/SoftKeyboardDetect(7141): Ignore this event
04-02 16:05:37.477: D/gralloc_goldfish(7141): Emulator without GPU emulation detected.
04-02 16:05:37.657: D/SoftKeyboardDetect(7141): We are in our onMeasure method
04-02 16:05:37.657: D/SoftKeyboardDetect(7141): Old Height = 762
04-02 16:05:37.657: D/SoftKeyboardDetect(7141): Height = 762
04-02 16:05:37.667: D/SoftKeyboardDetect(7141): Old Width = 480
04-02 16:05:37.667: D/SoftKeyboardDetect(7141): Width = 480
04-02 16:05:37.667: D/SoftKeyboardDetect(7141): Ignore this event
04-02 16:05:38.698: E/SQLiteLog(7141): (14) cannot open file at line 30174 of [00bb9c9ce4]
04-02 16:05:38.698: E/SQLiteLog(7141): (14) os_unix.c:30174: (2) open(/CachedGeoposition.db) - 
04-02 16:05:38.707: D/WebKit(7141): ERROR: 
04-02 16:05:38.707: D/WebKit(7141): SQLite database failed to load from /CachedGeoposition.db
04-02 16:05:38.707: D/WebKit(7141): Cause - unable to open database file
04-02 16:05:38.707: D/WebKit(7141): external/webkit/Source/WebCore/platform/sql/SQLiteDatabase.cpp(71) : bool WebCore::SQLiteDatabase::open(const WTF::String&, bool)
04-02 16:05:38.907: D/PhoneGapLog(7141): null: Line 1 : XMLHttpRequest cannot load http://127.0.0.1:36419/01c9cf14-1235-4458-9b68-33a094aad126. Origin null is not allowed by Access-Control-Allow-Origin.
04-02 16:05:38.917: E/Web Console(7141): XMLHttpRequest cannot load http://127.0.0.1:36419/01c9cf14-1235-4458-9b68-33a094aad126. Origin null is not allowed by Access-Control-Allow-Origin. at null:1
04-02 16:05:38.987: D/PhoneGapLog(7141): file:///android_asset/www/phonegap-1.1.0.js: Line 930 : JSCallback Error: Request failed.
04-02 16:05:38.997: I/Web Console(7141): JSCallback Error: Request failed. at file:///android_asset/www/phonegap-1.1.0.js:930
04-02 16:05:39.218: D/PhoneGapLog(7141): file:///android_asset/www/phonegap-1.1.0.js: Line 769 : Error in success callback: Network Status1 = TypeError: Cannot read property 'contacts' of undefined
04-02 16:05:39.218: I/Web Console(7141): Error in success callback: Network Status1 = TypeError: Cannot read property 'contacts' of undefined at file:///android_asset/www/phonegap-1.1.0.js:769
04-02 16:05:39.347: D/dalvikvm(7141): GC_CONCURRENT freed 210K, 4% free 8192K/8455K, paused 18ms+5ms, total 58ms

任何帮助将不胜感激。

感谢。

2 个答案:

答案 0 :(得分:0)

您是否向AndroidManifest.xml添加了READ_CONTACTS权限?

代码:

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

我建议你删除功能init(),然后让:

document.addEventListener("deviceready",onDeviceReady,false);

原因是init()未被触发。

如果您执行了所有Phongap步骤并且没有出现错误,那么您必须尝试最后一次认为过去对我有用,只需保存您的项目数据并执行phonegap buildphonegap build app_platform

答案 1 :(得分:0)

检查你的phoneNumbers数组是否为空

相关问题