0

我正在编写一个显示来自网站的数据的小应用程序。数据以 json 字符串形式提供。我有 3 项活动很好。现在我更改了一项活动的代码并想对其进行测试。但从那时起,所有活动都不再起作用,我没有发现调试器的错误。也许有人可以帮助我使用 logcat。

谢谢

08-23 08:17:09.600: E/Trace(641): error opening trace file: No such file or directory (2)
08-23 08:17:10.368: D/dalvikvm(641): GC_FOR_ALLOC freed 93K, 2% free 10954K/11143K, paused 60ms, total 64ms
08-23 08:17:10.738: D/gralloc_goldfish(641): Emulator without GPU emulation detected.
08-23 08:17:32.118: D/AndroidRuntime(641): Shutting down VM
08-23 08:17:32.118: W/dalvikvm(641): threadid=1: thread exiting with uncaughtexception (group=0x40a13300)
08-23 08:17:32.260: E/AndroidRuntime(641): FATAL EXCEPTION: main
08-23 08:17:32.260: E/AndroidRuntime(641): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.damps.fantasy/de.damps.fantasy.scoresActivity}: android.os.NetworkOnMainThreadException
08-23 08:17:32.260: E/AndroidRuntime(641):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-23 08:17:32.260: E/AndroidRuntime(641):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-23 08:17:32.260: E/AndroidRuntime(641):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-23 08:17:32.260: E/AndroidRuntime(641):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-23 08:17:32.260: E/AndroidRuntime(641):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 08:17:32.260: E/AndroidRuntime(641):  at android.os.Looper.loop(Looper.java:137)
08-23 08:17:32.260: E/AndroidRuntime(641):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-23 08:17:32.260: E/AndroidRuntime(641):  at java.lang.reflect.Method.invokeNative(Native Method)
08-23 08:17:32.260: E/AndroidRuntime(641):  at java.lang.reflect.Method.invoke(Method.java:511)
08-23 08:17:32.260: E/AndroidRuntime(641):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-23 08:17:32.260: E/AndroidRuntime(641):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-23 08:17:32.260: E/AndroidRuntime(641):  at dalvik.system.NativeStart.main(Native Method)
08-23 08:17:32.260: E/AndroidRuntime(641): Caused by: android.os.NetworkOnMainThreadException
08-23 08:17:32.260: E/AndroidRuntime(641):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
08-23 08:17:32.260: E/AndroidRuntime(641):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-23 08:17:32.260: E/AndroidRuntime(641):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-23 08:17:32.260: E/AndroidRuntime(641):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-23 08:17:32.260: E/AndroidRuntime(641):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
08-23 08:17:32.260: E/AndroidRuntime(641):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
08-23 08:17:32.260: E/AndroidRuntime(641):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
08-23 08:17:32.260: E/AndroidRuntime(641):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
08-23 08:17:32.260: E/AndroidRuntime(641):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-23 08:17:32.260: E/AndroidRuntime(641):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
08-23 08:17:32.260: E/AndroidRuntime(641):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
08-23 08:17:32.260: E/AndroidRuntime(641):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
08-23 08:17:32.260: E/AndroidRuntime(641):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
08-23 08:17:32.260: E/AndroidRuntime(641):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
08-23 08:17:32.260: E/AndroidRuntime(641):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
08-23 08:17:32.260: E/AndroidRuntime(641):  at java.net.URL.openStream(URL.java:462)
08-23 08:17:32.260: E/AndroidRuntime(641):  at de.damps.fantasy.Json.readUrl(Json.java:39)
08-23 08:17:32.260: E/AndroidRuntime(641):  at de.damps.fantasy.Json.<init>(Json.java:20)
08-23 08:17:32.260: E/AndroidRuntime(641):  at de.damps.fantasy.scoresActivity.parse(scoresActivity.java:110)
08-23 08:17:32.260: E/AndroidRuntime(641):  at de.damps.fantasy.scoresActivity.onCreate(scoresActivity.java:34)
08-23 08:17:32.260: E/AndroidRuntime(641):  at android.app.Activity.performCreate(Activity.java:5008)
08-23 08:17:32.260: E/AndroidRuntime(641):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-23 08:17:32.260: E/AndroidRuntime(641):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-23 08:17:32.260: E/AndroidRuntime(641):  ... 11 more
08-23 08:17:32.399: D/dalvikvm(641): GC_CONCURRENT freed 227K, 3% free 11198K/11463K, paused 77ms+106ms, total 270ms
4

3 回答 3

0

当您从网站下载数据时,您必须实现一个 AsynTask。

例子 :

使用 AsyncTask :

public class MyAsyncTask extends AsyncTask<Void, Void, Result>{
                    private Activity activity;
                    private ProgressDialog progressDialog;

        public MyAsyncTask(Activity activity) {
                        super();
            this.activity = activity;
        }

        @Override
        protected void onPreExecute() {
        super.onPreExecute();
            progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true);
        }

        @Override
        protected Result doInBackground(Void... v) {
        //do your stuff here
        return null;

        }

        @Override
        protected void onPostExecute(Result result) {
            progressDialog.dismiss();
            Toast.makeText(activity.getApplicationContext(), "Finished.", 
                    Toast.LENGTH_LONG).show();
        }

}

从活动中调用它:

MyAsyncTask task = new AsyncTask(myActivity.this);
task.execute();
于 2012-08-23T08:47:26.013 回答
0

您正在主 UI 线程上的该 Activity 中执行一些网络活动。每当您的构建目标设置为蜂窝或更高时,这将引发异常。要修复它,请减少构建目标,或将网络操作移动到它自己的ThreadAsyncTask

于 2012-08-23T08:46:18.473 回答
0

您正在 UI 线程中进行一些网络操作,避免长时间运行不需要 UI 的任务,在其他线程中,通过以下链接可以实现此目的:

http://www.vogella.com/articles/AndroidPerformance/article.html

于 2012-08-23T08:42:12.250 回答