调用另一个活动时app崩溃(提供logcat)

时间:2012-08-23 08:39:25

标签: android logcat

我正在编写一个显示网站数据的小应用程序。数据以json字符串形式提供。我有3个活动很好。现在我改变了一个活动的代码,并想测试它。但从那时起,没有任何活动正在运行,我没有找到调试器的错误。也许有人可以帮我解决这个问题。

THX

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

3 个答案:

答案 0 :(得分:0)

你正在UI线程中进行一些网络操作,避免长时间运行不需要UI的任务,在其他一些线程中,通过以下链接你可以做到这一点:

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

答案 1 :(得分:0)

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

答案 2 :(得分: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();