MainActivity泄露了最初添加的窗口com.android.internal.policy.impl.PhoneWindow$DecorView@413da0c0

时间:2013-10-09 21:17:46

标签: java android memory-leaks logcat forceclose

我在日食中遇到了一个问题:

MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@413da0c0 that was originally added here

有没有人见过这个或知道如何解决这个问题?我已经查看了很多次代码,我根本不明白可以做些什么来解决它。

logcat的:

10-09 17:12:15.630: E/WindowManager(1829): Activity com.example.test.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@413da0c0 that was originally added here
10-09 17:12:15.630: E/WindowManager(1829): android.view.WindowLeaked: Activity com.example.test.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@413da0c0 that was originally added here
10-09 17:12:15.630: E/WindowManager(1829):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
10-09 17:12:15.630: E/WindowManager(1829):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
10-09 17:12:15.630: E/WindowManager(1829):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
10-09 17:12:15.630: E/WindowManager(1829):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
10-09 17:12:15.630: E/WindowManager(1829):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
10-09 17:12:15.630: E/WindowManager(1829):  at android.app.Dialog.show(Dialog.java:278)
10-09 17:12:15.630: E/WindowManager(1829):  at com.example.test.MainActivity$MyTask.onPreExecute(MainActivity.java:56)
10-09 17:12:15.630: E/WindowManager(1829):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
10-09 17:12:15.630: E/WindowManager(1829):  at android.os.AsyncTask.execute(AsyncTask.java:511)
10-09 17:12:15.630: E/WindowManager(1829):  at com.example.test.MainActivity.onCreate(MainActivity.java:43)
10-09 17:12:15.630: E/WindowManager(1829):  at android.app.Activity.performCreate(Activity.java:4465)
10-09 17:12:15.630: E/WindowManager(1829):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-09 17:12:15.630: E/WindowManager(1829):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1923)
10-09 17:12:15.630: E/WindowManager(1829):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1984)
10-09 17:12:15.630: E/WindowManager(1829):  at android.app.ActivityThread.access$600(ActivityThread.java:126)
10-09 17:12:15.630: E/WindowManager(1829):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1150)
10-09 17:12:15.630: E/WindowManager(1829):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 17:12:15.630: E/WindowManager(1829):  at android.os.Looper.loop(Looper.java:137)
10-09 17:12:15.630: E/WindowManager(1829):  at android.app.ActivityThread.main(ActivityThread.java:4456)
10-09 17:12:15.630: E/WindowManager(1829):  at java.lang.reflect.Method.invokeNative(Native Method)
10-09 17:12:15.630: E/WindowManager(1829):  at java.lang.reflect.Method.invoke(Method.java:511)
10-09 17:12:15.630: E/WindowManager(1829):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
10-09 17:12:15.630: E/WindowManager(1829):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
10-09 17:12:15.630: E/WindowManager(1829):  at dalvik.system.NativeStart.main(Native Method)

来源:

public class MainActivity extends Activity {

    TextView tv;
    String url = "http://examplecloudurl.org/apps/users/results.cfm?lname=BAR&fname=FOO";
    String tr;
    Document doc;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.TextView01);
        new MyTask().execute(url);
    }

    private class MyTask extends AsyncTask<String, Void, String> {

        ProgressDialog prog;

        String title = "";

        @Override
        protected void onPreExecute() {
            prog = new ProgressDialog(MainActivity.this);
            prog.setMessage("Loading....");
            prog.show();

        }

        @Override
        protected String doInBackground(String... params) {

            ImageView img = (ImageView) findViewById(R.id.imageView1);
            {
                try {




                    String imageURL = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg";
                    HttpGet httpRequest = null;

                    httpRequest = new HttpGet(URI.create(imageURL));

                    HttpClient httpclient = new DefaultHttpClient();
                    HttpResponse response = (HttpResponse) httpclient
                            .execute(httpRequest);

                    HttpEntity entity = response.getEntity();
                    BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
                    InputStream input = b_entity.getContent();

                    Bitmap bitmap = BitmapFactory.decodeStream(input);

                    img.setImageBitmap(bitmap);

                    doc = Jsoup.connect(params[0]).get();
                    Element tableElement = doc.select(".datagrid").first();

                    Elements tableRows = tableElement.select("tr");
                    for (Element row : tableRows) {
                        Elements cells = row.select("td");
                        if (cells.size() > 0) {
                            title = cells.get(0).child(0).attr("href") + " ; "
                                    + cells.get(0).text() + "; "
                                    + cells.get(1).text() + "; "
                                    + cells.get(2).text() + "; "
                                    + cells.get(3).text();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return title;
            }
        }


        public void main(String[] args) throws IOException
        {
            String url = "http://www.juventus.com/wps/poc?uri=wcm:oid:91da6dbb-4089-49c0-a1df-3a56671b7020";

            Document document = manuelRedirectHandler(url);

            Elements elements = document.getElementsByClass("juveShareImage");

            for (Element element : elements)
            {
                System.out.println(element.attr("src"));
            }

        }

        private Document manuelRedirectHandler(String url) throws IOException
        {
            org.jsoup.Connection.Response response = Jsoup.connect(url.replaceAll(" ", "%20")).followRedirects(false).execute();
            int status = response.statusCode();

            if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)
            {
                String redirectUrl = response.header("location");
                System.out.println("Redirect to: " + redirectUrl);
                return manuelRedirectHandler(redirectUrl);
            }

            return Jsoup.parse(response.body());
        }

        @Override
        protected void onPostExecute(String title) {
            super.onPostExecute(title);
            prog.dismiss();
            tv.setText(title);

        }

    }
}
编辑(第一次回复后):

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (TextView) findViewById(R.id.TextView01);
    new MyTask().execute(url);
}

private class MyTask extends AsyncTask<String, Void, String> {

    ProgressDialog prog;

    String title = "";

    @Override
    protected void onPreExecute() {
        prog = new ProgressDialog(MainActivity.this);
        prog.setMessage("Loading....");
        prog.show();

    }

    @Override
    protected String doInBackground(String... params) {


        {
            return title;
        }
    }


    public void main(String[] args) throws IOException
    {
        String url = "http://www.juventus.com/wps/poc?uri=wcm:oid:91da6dbb-4089-49c0-a1df-3a56671b7020";

        Document document = manuelRedirectHandler(url);

        Elements elements = document.getElementsByClass("juveShareImage");

        for (Element element : elements)
        {
            System.out.println(element.attr("src"));
        }

    }

    private Document manuelRedirectHandler(String url) throws IOException
    {
        org.jsoup.Connection.Response response = Jsoup.connect(url.replaceAll(" ", "%20")).followRedirects(false).execute();
        int status = response.statusCode();

        if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)
        {
            String redirectUrl = response.header("location");
            System.out.println("Redirect to: " + redirectUrl);
            return manuelRedirectHandler(redirectUrl);
        }

        return Jsoup.parse(response.body());
    }

    protected void onPostExecute(String... params) {
        super.onPostExecute(title);
        prog.dismiss();
        tv.setText(title);

        try {




            String imageURL = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg";
            HttpGet httpRequest = null;

            httpRequest = new HttpGet(URI.create(imageURL));

            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = (HttpResponse) httpclient
                    .execute(httpRequest);

            HttpEntity entity = response.getEntity();
            BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
            InputStream input = b_entity.getContent();

            Bitmap bitmap = BitmapFactory.decodeStream(input);

            img.setImageBitmap(bitmap);

            doc = Jsoup.connect(params[0]).get();
            Element tableElement = doc.select(".datagrid").first();

            Elements tableRows = tableElement.select("tr");
            for (Element row : tableRows) {
                Elements cells = row.select("td");
                if (cells.size() > 0) {
                    title = cells.get(0).child(0).attr("href") + " ; "
                            + cells.get(0).text() + "; "
                            + cells.get(1).text() + "; "
                            + cells.get(2).text() + "; "
                            + cells.get(3).text();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

}

1 个答案:

答案 0 :(得分:0)

您正在抓取ImageView并尝试在AsyncTask的doInBackground中对其进行操作。这是禁忌。

这个及其所有引用都不应该在doInBackground()中:

ImageView img = (ImageView) findViewById(R.id.imageView1);

阅读Android documentation for AsyncTask并进行一些谷歌搜索。

您基本上想要做的是将doInBackground()中的位图返回到AsyncTask的postExecute()。然后,您可以使用位图设置ImageView。

为什么你在android AsyncTask类中有一个main()?