?
[size=13px;]<span style="font-family: courier new, courier;">[size=16px;]
错误信息[/size]<br>[/size]</span>
[size=13px;]<span style="font-family: courier new, courier;">??? 最近在学Android使用KSOAP2调用现有的
Webservice的方法,期间在网上找了很多代码,但是每次在Android模拟器中运行的时候程序都会崩溃,搞得自己都快崩溃了。。。。[/size]</span>
[size=13px;]<span style="font-family: courier new, courier;">??? 查看了一下LogCat中的错误报告,
发现是因为java.lang.NoClassDefFoundError导致程序崩溃的:[/size]<br></span>
<div class="dp-highlighter bg_java">
<div class="bar">
<div class="tools">
<a class="ViewSource" title="view plain" href="http://topmanopensource.iteye.com/admin/b
logs/#">view plain</a><a class="CopyToClipboard" title="copy to clipboard" href="http://topmanopensource.iteye.com/admin/blogs/#">copy to clipboard</a><a class="PrintSource" title="print" href="http://topmanopensource.iteye.com/admin/blogs/#">print</a><a class="About" title="?" href="http://topmanopensource.iteye.com/admin/blogs/#">?</a>
<ol class="dp-j">
<li class="alt"><span><span>ERROR/AndroidRuntime(</span><span class="number">600</span><span>):?java.lang.NoClassDefFoundError:?org.ksoap2.serialization.SoapObject????</span></span></li>
<li>
<span>????at?webservice.weather.WeatherActivity.getWeather(WeatherActivity.java:</span><span class="number">104</span><span>)????</span>
</li>
<li class="alt">
<span>????at?webservice.weather.WeatherActivity.showWeather(WeatherActivity.java:</span><span class="number">84</span><span>)????</span>
</li>
<li>
<span>????at?webservice.weather.WeatherActivity.access$</span><span class="number">0</span><span>(WeatherActivity.java:</span><span class="number">80</span><span>)????</span>
</li>
<li class="alt">
<span>????at?webservice.weather.WeatherActivity$</span><span class="number">1</span><span>.onClick(WeatherActivity.java:</span><span class="number">75</span><span>)????</span>
</li>
<li>
<span>????at?android.view.View.performClick(View.java:</span><span class="number">2408</span><span>)????</span>
</li>
<li class="alt">
<span>????at?android.view.View$PerformClick.run(View.java:</span><span class="number">8816</span><span>)????</span>
</li>
<li>
<span>????at?android.os.Handler.handleCallback(Handler.java:</span><span class="number">587</span><span>)????</span>
</li>
<li class="alt">
<span>????at?android.os.Handler.dispatchMessage(Handler.java:</span><span class="number">92</span><span>)????</span>
</li>
<li>
<span>????at?android.os.Looper.loop(Looper.java:</span><span class="number">123</span><span>)????</span>
</li>
<li class="alt">
<span>????at?android.app.ActivityThread.main(ActivityThread.java:</span><span class="number">4627</span><span>)????</span>
</li>
<span>????at?java.lang.reflect.Method.invokeNative(Native?Method)????</span>
<li class="alt">
<span>????at?java.lang.reflect.Method.invoke(Method.java:</span><span class="number">521</span><span>)????</span>
</li>
<li>
<span>????at?com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:</span><span class="number">868</span><span>)????</span>
</li>
<li class="alt">
<span>????at?com.android.internal.os.ZygoteInit.main(ZygoteInit.java:</span><span class="number">626</span><span>)????</span>
</li>
<span>????at?dalvik.system.NativeStart.main(Native?Method)??</span>
</ol>
<textarea class="java" style="display: none;" cols="20" rows="2" name="code" readonly>ERROR/AndroidRuntime(600): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
at webservice.weather.WeatherActivity.getWeather(WeatherActivity.java:104)
at webservice.weather.WeatherActivity.showWeather(WeatherActivity.java:84)
at webservice.weather.WeatherActivity.access$0(WeatherActivity.java:80)
at webservice.weather.WeatherActivity$1.onClick(WeatherActivity.java:75)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)</textarea>
?
[size=16px;]
解决方法[/size]<br>[size=13px;]</span>
[size=13px;]<span style="font-family: courier new, courier;">找了很多资料,最后才发现是jar包的导入有问题。。。。以前开发java程序时导入jar包都是用一下的方法:[/size][/size]
[size=13px;]<span style="font-family: courier new, courier;">??? 工程点击右键->Build Path->Add Libraries->User Library->选择包含需要的jar包的Library(如果没有的话就点击User Libraries新建一个Library,然后再选中)[/size]</span>
[size=13px;]<span style="font-family: courier new, courier;">但是,在Android中这样导入jar包会导致程序在模拟器中运行时抛出[/size]java.lang.NoClassDefFoundError
异常,在Android中导入jar包的步骤如下:</span>
[size=13px;]????? <span style="color: #ff0000;">
工程点击右键->[/size]</span>[size=13px;]<span style="font-family: courier new, courier;"><span style="color: #ff0000;">
Build Path->Add External Archives->选择要导入的jar包[/size]</span></span>
[size=13px;]<span style="font-family: courier new, courier;"><span style="color: black;"><span style="color: black;">另外还有一个问题就是:Android模拟器访问Tomcat上部署的webservice程序时,不能用localhost或者本机IP,Android默认访问本机地址为10.0.2.2。[/size]</span></span></span>
[size=13px;]<span style="font-family: courier new, courier;"><span style="color: #ff0000;">
<span style="color: #000000; font-size: 16px;">原因猜想[/size]</span></span></span>
[size=13px;]<span style="font-family: courier new, courier;"><span style="color: #ff0000;"><span style="color: #000000;">??? 对于产生上述NoClassDefFoundError的原因,查看一下工程中的classpath文件就可以找到。如果是利用第一种方法导入的jar包的话,classpath文件中会生成这样一条语句:<span style="color: #ff0000;"><classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/KSOAP2"/>[/size],这种导入jar包的方法依赖于开发环境eclipse;而如果用第二种方法导入jar包的话,会在classpath中产生如下的语句:</span></span></span></span>
[size=13px;]<span style="font-family: courier new, courier;"><span style="color: #ff0000;"><span style="color: #000000;"><span style="color: #ff0000;"><classpathentry kind="lib" path="D:/JAVATOOLS/ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar"/>,<span style="color: #000000;">这种导入方式是用的绝对路径,与eclipse开发环境无关,而且以这种方式导入后,jar包的信息会被加到工程class.dex中,进而会被打包到apk中。[/size]</span></span></span></span></span>
[size=13px;]<span style="font-family: courier new, courier;"><span style="color: #ff0000;"><span style="color: #000000;">??? 我猜想,在运行虚拟机的时候,虚拟机是脱离了eclipse开发环境的,所以第一种依赖于eclipse环境的导入方式会出现上述错误;而第二种导入方式由于不依靠eclipse,将jar包的信息放到了class.dex中,所以运行无误![/size]</span></span></span>