0

除了谷歌广告,我没有在我的活动中使用任何图片。但我得到 java.lang.outofmemoryerror 并且应用程序正在崩溃。

这是日志猫

03-25 18:24:22.125: E/AndroidRuntime(277): FATAL EXCEPTION: AsyncTask #2
03-25 18:24:22.125: E/AndroidRuntime(277): java.lang.RuntimeException: An error occured while executing doInBackground()
03-25 18:24:22.125: E/AndroidRuntime(277):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
03-25 18:24:22.125: E/AndroidRuntime(277):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-25 18:24:22.125: E/AndroidRuntime(277):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-25 18:24:22.125: E/AndroidRuntime(277):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-25 18:24:22.125: E/AndroidRuntime(277):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-25 18:24:22.125: E/AndroidRuntime(277):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-25 18:24:22.125: E/AndroidRuntime(277):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-25 18:24:22.125: E/AndroidRuntime(277):  at java.lang.Thread.run(Thread.java:1096)
03-25 18:24:22.125: E/AndroidRuntime(277): Caused by: java.lang.OutOfMemoryError
03-25 18:24:22.125: E/AndroidRuntime(277):  at org.kxml2.io.KXmlParser.push(KXmlParser.java:626)
03-25 18:24:22.125: E/AndroidRuntime(277):  at org.kxml2.io.KXmlParser.pushText(KXmlParser.java:865)
03-25 18:24:22.125: E/AndroidRuntime(277):  at org.kxml2.io.KXmlParser.nextImpl(KXmlParser.java:354)
03-25 18:24:22.125: E/AndroidRuntime(277):  at org.kxml2.io.KXmlParser.next(KXmlParser.java:1385)
03-25 18:24:22.125: E/AndroidRuntime(277):  at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:210)
03-25 18:24:22.125: E/AndroidRuntime(277):  at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:366)
03-25 18:24:22.125: E/AndroidRuntime(277):  at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:233)
03-25 18:24:22.125: E/AndroidRuntime(277):  at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:366)
03-25 18:24:22.125: E/AndroidRuntime(277):  at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:121)
03-25 18:24:22.125: E/AndroidRuntime(277):  at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:137)
03-25 18:24:22.125: E/AndroidRuntime(277):  at org.ksoap2.transport.Transport.parseResponse(Transport.java:63)
03-25 18:24:22.125: E/AndroidRuntime(277):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)
03-25 18:24:22.125: E/AndroidRuntime(277):  at com.netserv.Pungry.RestaurantsList.SendRequesttoServer(RestaurantsList.java:207)
03-25 18:24:22.125: E/AndroidRuntime(277):  at com.netserv.Pungry.RestaurantsList$ListData.doInBackground(RestaurantsList.java:148)
03-25 18:24:22.125: E/AndroidRuntime(277):  at com.netserv.Pungry.RestaurantsList$ListData.doInBackground(RestaurantsList.java:1)
03-25 18:24:22.125: E/AndroidRuntime(277):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-25 18:24:22.125: E/AndroidRuntime(277):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-25 18:24:22.125: E/AndroidRuntime(277):  ... 4 more
03-25 18:24:22.485: E/WindowManager(277): Activity com.netserv.Pungry.RestaurantsList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43f0d2e8 that was originally added here
03-25 18:24:22.485: E/WindowManager(277): android.view.WindowLeaked: Activity com.netserv.Pungry.RestaurantsList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43f0d2e8 that was originally added here
03-25 18:24:22.485: E/WindowManager(277):   at android.view.ViewRoot.<init>(ViewRoot.java:247)
03-25 18:24:22.485: E/WindowManager(277):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
03-25 18:24:22.485: E/WindowManager(277):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-25 18:24:22.485: E/WindowManager(277):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
03-25 18:24:22.485: E/WindowManager(277):   at android.app.Dialog.show(Dialog.java:241)
03-25 18:24:22.485: E/WindowManager(277):   at com.netserv.Pungry.RestaurantsList$ListData.onPreExecute(RestaurantsList.java:159)
03-25 18:24:22.485: E/WindowManager(277):   at android.os.AsyncTask.execute(AsyncTask.java:391)
03-25 18:24:22.485: E/WindowManager(277):   at com.netserv.Pungry.RestaurantsList.onCreate(RestaurantsList.java:83)
03-25 18:24:22.485: E/WindowManager(277):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-25 18:24:22.485: E/WindowManager(277):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-25 18:24:22.485: E/WindowManager(277):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-25 18:24:22.485: E/WindowManager(277):   at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-25 18:24:22.485: E/WindowManager(277):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-25 18:24:22.485: E/WindowManager(277):   at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 18:24:22.485: E/WindowManager(277):   at android.os.Looper.loop(Looper.java:123)
03-25 18:24:22.485: E/WindowManager(277):   at android.app.ActivityThread.main(ActivityThread.java:4627)
03-25 18:24:22.485: E/WindowManager(277):   at java.lang.reflect.Method.invokeNative(Native Method)
03-25 18:24:22.485: E/WindowManager(277):   at java.lang.reflect.Method.invoke(Method.java:521)
03-25 18:24:22.485: E/WindowManager(277):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-25 18:24:22.485: E/WindowManager(277):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-25 18:24:22.485: E/WindowManager(277):   at dalvik.system.NativeStart.main(Native Method)

这是我收到错误的 AsyncTask 类

public class ListData extends AsyncTask<String, Void, String> {

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        dialog.dismiss();
        try {
            if (!lstresponse.isEmpty()) {
                lstresult.addAll(lstresponse);
                GlobVariables.restaurant_response = lstresult;
                lst_restaurant.setAdapter(CustAdapter);
            } else
                Toast.makeText(getApplicationContext(),
                        "No result found for" + " " + check,
                        Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        SendRequesttoServer(params);
        return "1";
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        dialog = new ProgressDialog(RestaurantsList.this);
        dialog.setMessage("Please Wait...");
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();

    }

}

public void SendRequesttoServer(String[] params) {
    try {
        if (GlobVariables.MenuSelectd.equals("Cuisine")
                || GlobVariables.Selecteditem.equals("Cuisines")) {
            METHOD = "GetRestaurantsByCuisines";

            request = new SoapObject(NAMESPACE, METHOD);
            request.addProperty("cuisineName", params[0]);
            request.addProperty("cityName", params[1]);
        } else if (GlobVariables.MenuSelectd.equals("locations")
                || GlobVariables.Selecteditem.equals("Location")) {
            METHOD = "GetRestaurantsByLocations";
            request = new SoapObject(NAMESPACE, METHOD);
            request.addProperty("location", params[0]);
            request.addProperty("cityName", params[1]);
        } else if (GlobVariables.Selecteditem.equals("ByName")) {
            METHOD = "GetRestaurantsByName";
            request = new SoapObject(NAMESPACE, METHOD);
            request.addProperty("restaurantName", params[0]);
            request.addProperty("cityName", params[1]);

        } else {
            METHOD = "SearchRestaurant";
            request = new SoapObject(NAMESPACE, METHOD);
            request.addProperty("searchParameter", params[0]);
            request.addProperty("cityName", params[1]);
        }

        SOAP_ACTION = NAMESPACE + METHOD;

        // SoapObject request = new SoapObject(NAMESPACE, METHOD);
        // request.addProperty("location", params[0]);
        // request.addProperty("cityName", params[1]);
        check = params[0];
        SoapSerializationEnvelope res = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        res.dotNet = true;
        res.setOutputSoapObject(request);

        HttpTransportSE call = new HttpTransportSE(url);

        SoapPrimitive result;
        call.call(SOAP_ACTION, res);
        result = (SoapPrimitive) res.getResponse();
        ParseDetails Objparsecities = new ParseDetails(
                new ByteArrayInputStream(result.toString()
                        .getBytes("UTF-8")));
        lstresponse = Objparsecities.parse();
        System.out.println(lstresponse.size());

    } catch (SoapFault e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

我能知道为什么我收到 java.lang.outofmemoy 错误以及我在做什么错误吗?

谢谢:)

4

2 回答 2

0

在 Android 中,每个应用程序都分配有一个VM 预算。VM 预算是任何应用程序可以使用的最大内存量。此内存限制包括所有内容、位图(即使它是在外部堆上创建的)、对象等。每当应用程序超出此限制时,就会引发 OOM 异常。

VM 预算的值范围从 16MB 到 80MB 或更多。这取决于设备。

即使打印大量日志也会导致 OOM 问题,我自己已经测试过了。

同样在您的情况下,日志清楚地表明您在onPreExecute异步任务中添加的对话框正在泄漏。您是否正在处理活动中的方向变化?

03-25 18:24:22.485: E/WindowManager(277): 活动 com.netserv.Pungry.RestaurantsList 泄露了最初添加在这里的窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@43f0d2e8

03-25 18:24:22.485: E/WindowManager(277): android.view.WindowLeaked: Activity com.netserv.Pungry.RestaurantsList 泄露了窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@43f0d2e8最初添加在这里

如果您不处理方向更改,那么对于方向的每次更改,将再次创建并onCreate执行活动,这会导致异步任务被一次又一次地触发。这也可能导致更高的内存利用率。

你的服务器响应有多大,我有过服务器响应的经验,它有 50,000 多个节点,从长远来看会导致问题。

您是否使用外部库来解析 XML 数据?我建议编写一个简单的 SAX 解析器而不是那个,它会减少对象创建的数量,并且控制保留在您自己的代码中

于 2013-03-25T13:22:49.337 回答
0

问题很可能是您使用 ksoap2 通过 SOAP 请求发送/接收大量数据。OutOfMemoryError每当超出内存时就会引起。您的应用程序中没有必要必须存在图像才能发生这种情况。

以前曾报告过使用此库的此类问题。此线程中的解决方案可能对您有用:

非常大的 SOAP 响应 - Android - 内存不足错误

于 2013-03-25T13:25:09.747 回答