不幸的是,[App Name]已停止

时间:2013-06-25 10:14:55

标签: android

我正在开发一个获取实时货币汇率的Android应用程序。但不幸的是,它已经停止了工作。

以下是日志:

06-25 09:57:15.881: D/dalvikvm(788): GC_CONCURRENT freed 77K, 7% free 2737K/2932K, paused 17ms+34ms, total 107ms
06-25 09:57:16.071: D/gralloc_goldfish(788): Emulator without GPU emulation detected.
06-25 09:57:28.326: D/dalvikvm(788): GC_FOR_ALLOC freed 13K, 5% free 3042K/3192K, paused 25ms, total 28ms
06-25 09:57:28.336: I/dalvikvm-heap(788): Grow heap (frag case) to 4.159MB for 1127536-byte allocation
06-25 09:57:28.456: D/dalvikvm(788): GC_FOR_ALLOC freed 4K, 4% free 4139K/4296K, paused 118ms, total 118ms
06-25 09:57:28.518: D/dalvikvm(788): GC_CONCURRENT freed 1K, 4% free 4157K/4296K, paused 4ms+3ms, total 62ms
06-25 09:57:32.847: D/AndroidRuntime(788): Shutting down VM
06-25 09:57:32.847: W/dalvikvm(788): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-25 09:57:32.856: E/AndroidRuntime(788): FATAL EXCEPTION: main
06-25 09:57:32.856: E/AndroidRuntime(788): android.os.NetworkOnMainThreadException
06-25 09:57:32.856: E/AndroidRuntime(788):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-25 09:57:32.856: E/AndroidRuntime(788):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-25 09:57:32.856: E/AndroidRuntime(788):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-25 09:57:32.856: E/AndroidRuntime(788):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-25 09:57:32.856: E/AndroidRuntime(788):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-25 09:57:32.856: E/AndroidRuntime(788):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-25 09:57:32.856: E/AndroidRuntime(788):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-25 09:57:32.856: E/AndroidRuntime(788):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-25 09:57:32.856: E/AndroidRuntime(788):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-25 09:57:32.856: E/AndroidRuntime(788):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-25 09:57:32.856: E/AndroidRuntime(788):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-25 09:57:32.856: E/AndroidRuntime(788):  at com.example.allcurrency.MainActivity.getJson(MainActivity.java:91)
06-25 09:57:32.856: E/AndroidRuntime(788):  at com.example.allcurrency.MainActivity$1.onClick(MainActivity.java:66)
06-25 09:57:32.856: E/AndroidRuntime(788):  at android.view.View.performClick(View.java:4204)
06-25 09:57:32.856: E/AndroidRuntime(788):  at android.view.View$PerformClick.run(View.java:17355)
06-25 09:57:32.856: E/AndroidRuntime(788):  at android.os.Handler.handleCallback(Handler.java:725)
06-25 09:57:32.856: E/AndroidRuntime(788):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 09:57:32.856: E/AndroidRuntime(788):  at android.os.Looper.loop(Looper.java:137)
06-25 09:57:32.856: E/AndroidRuntime(788):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-25 09:57:32.856: E/AndroidRuntime(788):  at java.lang.reflect.Method.invokeNative(Native Method)
06-25 09:57:32.856: E/AndroidRuntime(788):  at java.lang.reflect.Method.invoke(Method.java:511)
06-25 09:57:32.856: E/AndroidRuntime(788):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-25 09:57:32.856: E/AndroidRuntime(788):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-25 09:57:32.856: E/AndroidRuntime(788):  at dalvik.system.NativeStart.main(Native Method)
06-25 09:58:27.896: I/Process(788): Sending signal. PID: 788 SIG: 9

4 个答案:

答案 0 :(得分:3)

当您尝试在主UI线程中执行网络操作时,会出现

NetworkOnMainThreadException。使用AsyncTask代替网络操作

答案 1 :(得分:2)

由于严格的模式政策导致应用崩溃,在您向服务发送请求或连接到网络之前插入以下给定的行:

        ThreadPolicy tp = ThreadPolicy.LAX;
        StrictMode.setThreadPolicy(tp);

希望它能帮助你:)

答案 2 :(得分:1)

当应用程序尝试在其主线程上执行网络操作时,抛出此异常。在AsyncTask中运行您的代码。

不要忘记将其添加到AndroidManifest.xml文件中:

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

查看Painless Threading

上的文章

答案 3 :(得分:1)

使用Asynctask进行网络计算或任何耗时的任务。

你不应该使用UI线程进行任何繁重的计算,因为它会使你的UI无响应,你在UI Thraed中做了一些任务,这就是你得到这个异常的原因