您的位置:首页 > 移动开发 > Android开发

一步一步学习基本PhoneGap的Android应用程序

2013-06-29 17:34 309 查看
本教程假定你使用Eclipse进行android开发,你可以创建新的Android项目。 PhoneGap的文件名为cordova。

1.创建新的Android项目在eclipse

2.从http://phonegap.com/download/下载cordova-2.6.0.jar这个zip压缩包,并解压

3.将cordova-2.6.0.jar添加到项目的构建路径。在Eclipse中打开项目properties > Java Build Path > Add Jars

4.更改main Activity的父类为DroidGap

5.用super.loadUrl(“file:///android_asset/www/index.html”);来代替setContentView(R.layout.main);

6.在项目的res文件夹中创建文件夹xml。从之前下载的软件包中添加plugins.xml和cordova.xml。

7.把<uses-permission android:name="android.permission.INTERNET"/>和<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>添加到AndroidManifest.xml中

8.在项目的assets文件夹中,创建www文件夹

9.把index.html文件添加到assets/www文件夹中。文件内容是常规的HMTL。

现在,你已经可以运行的程序。接下来的步骤开始使用PhoneGap的特殊的HTML5特性,并尝试访问硬件。如果无法运行,那么要确保你有logcat的视图在eclipse激活或尝试在linux的命令行:

./adb logcat
ERROR:
W/System.err(24831): ERROR: plugin.xml is missing.  Add res/xml/plugins.xml to your project.
W/System.err(24831): https://raw.github.com/phonegap/phonegap-android/master/framework/res/xml/plugins.xml SOLUTION:
Make sure you have plugins.xml in projects res/xml folder.
ERROR:
W/System.err(24831): java.net.SocketException: Permission denied
W/System.err(24831):     at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method)
W/System.err(24831):     at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335)
W/System.err(24831):     at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:219)
W/System.err(24831):     at org.apache.harmony.luni.net.PlainServerSocketImpl.create(PlainServerSocketImpl.java:40)
W/System.err(24831):     at java.net.ServerSocket.<init>(ServerSocket.java:120)
W/System.err(24831):     at java.net.ServerSocket.<init>(ServerSocket.java:74)
W/System.err(24831):     at com.phonegap.CallbackServer.run(CallbackServer.java:208)
W/System.err(24831):     at java.lang.Thread.run(Thread.java:1027)
SOLUTION:
Add <uses-permission android:name="android.permission.INTERNET" /> permission
ERROR:
E/AndroidRuntime(24831): FATAL EXCEPTION: main
E/AndroidRuntime(24831): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobiwsale.client/com.mobiwsale.client.MobiwsaleClientActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
E/AndroidRuntime(24831):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
E/AndroidRuntime(24831):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
E/AndroidRuntime(24831):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
E/AndroidRuntime(24831):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
E/AndroidRuntime(24831):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(24831):     at android.os.Looper.loop(Looper.java:150)
E/AndroidRuntime(24831):     at android.app.ActivityThread.main(ActivityThread.java:4277)
E/AndroidRuntime(24831):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(24831):     at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(24831):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(24831):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(24831):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(24831): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
E/AndroidRuntime(24831):     at android.content.res.Resources.getValue(Resources.java:901)
E/AndroidRuntime(24831):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1887)
E/AndroidRuntime(24831):     at android.content.res.Resources.getXml(Resources.java:789)
E/AndroidRuntime(24831):     at com.phonegap.api.PluginManager.loadPlugins(PluginManager.java:82)
E/AndroidRuntime(24831):     at com.phonegap.api.PluginManager.<init>(PluginManager.java:62)
E/AndroidRuntime(24831):     at com.phonegap.DroidGap$1.run(DroidGap.java:414)
E/AndroidRuntime(24831):     at android.app.Activity.runOnUiThread(Activity.java:3878)
E/AndroidRuntime(24831):     at com.phonegap.DroidGap.loadUrlIntoView(DroidGap.java:390)
E/AndroidRuntime(24831):     at com.phonegap.DroidGap.loadUrl(DroidGap.java:356)
E/AndroidRuntime(24831):     at com.mobiwsale.client.MobiwsaleClientActivity.onCreate(MobiwsaleClientActivity.java:11)
E/AndroidRuntime(24831):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
E/AndroidRuntime(24831):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
E/AndroidRuntime(24831):     ... 11 more
SOLUTION:
Check plugin.xml and phonegap.xml files in res/xml
ERROR:
D/DroidGap(24990): DroidGap: GapViewClient.onReceivedError: Error code=-14 Description=The requested file was not found. www/index.html URL=file:///android_asset/www/index.html
SOLUTION
add index.html to to folder assets/www

ERROR:
E/AndroidRuntime( 4320): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x10000000 (has extras) } in org.apache.cordova.NetworkManager$1@4053d928
SOLUTION:
Add <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> permission

See also http://hiediutley.com/2011/10/28/hellophonegap1-0-project-updated-for-phonegap-1-1-0/[/code]原文地址: href="http://marguspala.com/basic-phonegap-android-app-from-scratch-step-by-step/" target=_blank>http://marguspala.com/basic-phonegap-android-app-from-scratch-step-by-step/
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: