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

标签 java android memory-leaks logcat forceclose

我在 Eclipse 中遇到一个问题:

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): 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();
        }

    }

}

}

最佳答案

您正在抓取一个 ImageView 并尝试在 AsyncTask 的 doInBackground 中操作它。这是禁忌。

此内容以及对它的所有引用不应出现在 doInBackground() 中:

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

通读Android documentation for AsyncTask并通过谷歌搜索一些示例。

您基本上想要做的是将位图从 doInBackground() 返回到 AsyncTask 的 postExecute() 。然后您可以在那里设置 ImageView 和位图。

为什么 Android AsyncTask 类中有一个 main() ?

关于java - MainActivity 泄露了最初在此处添加的窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@413da0c0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19282935/

相关文章:

java - Python 到 Java 加密 (RSA)

java - 为什么在 Java 中初始化数组对象时不需要提供括号?

java - Spring - 将依赖项注入(inject) ServletContextListener

memory-leaks - 什么是内存泄漏?

java - Spring Boot 应用程序中的 JVM "EXCEPTION_ACCESS_VIOLATION"崩溃

java - 关于Java HashMap的实现

android - GridView 中的 ImageView 高度

android - FCM 推送通知未打开应用程序

Android 在第一个应用程序使用时注册到远程服务器

java - 这段代码中是否存在资源(准备好的语句)泄漏?