android - MainActivity已泄露窗口

时间:2013-12-06 11:24:21

标签: android android-activity window memory-leaks

每当我从/向MainActivity按回来时,都会收到此错误消息。该应用程序不会崩溃,但此消息将显示在logcat中。我搜索过这个问题,但大多数都与解雇对话有关。但我的情况是我没有在MainActivity上显示对话框。

12-06 18:18:24.799  13422-13422/com.zq.x E/WindowManager﹕ android.view.WindowLeaked: Activity com.zq.x.MainActivity has leaked window android.widget.RelativeLayout{42e869e0 I.E..... ......ID 0,0-768,96} that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher.addHeaderViewToActivity(PullToRefreshAttacher.java:616)
            at uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher$1.run(PullToRefreshAttacher.java:127)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

activity_main.xml中

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" />

MainActivity.java

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

        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }

        session = new SessionManager(getApplicationContext());

        nearbyUsers = new ArrayList<User>();

        if(!session.isValidated()){ 
            session.loginUser();
            finish();
        }else{
            Log.d("MainActivity","Logged in as ["+session.getUserDetails().get(SessionManager.KEY_USR_EMAIL)+"]");

            gps = new GPSTracker(getApplicationContext());
            if(gps.canGetLocation()){
                gps.addListener(this);
                if(!gps.isNetworkEnabled() && gps.isGPSEnabled() && mUpdateLocTask == null){
                    this.locationUpdated();
                }
            }
        }
    }
...

1 个答案:

答案 0 :(得分:0)

我也有这个问题。做了一件事,它对我有用。

PullToRefreshLayout (扩展RelativeLayout)类实际上是将RelativeLayout动态添加到主活动中。我不明白它的一切。但是,我可以告诉你的一件事是,当用户退出主活动时,需要销毁RelativeLayout。

销毁相对布局实际上已经由PullToRefresh处理,但在我的情况下在某些时候失败了。我不得不在我的主要活动的onDetachedFromWindow()中手动销毁RelativeLayout视图。

我在我的库中修改了 PullToRefreshLayout.java 并添加了:

public void manuallyDetachFromWindow(){
    onDetachedFromWindow();
}

MainActivity.java 中,我添加了以下代码:

public static WeakReference<PullToRefreshLayout> wrefMPullToRefreshLayout;
...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    wrefMPullToRefreshLayout = new WeakReference<PullToRefreshLayout>(new PullToRefreshLayout(this));

}

@Override
public void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    try{
         MainActivity.wrefMPullToRefreshLayout.get().manuallyDetachFromWindow();
    }catch(NullPointerException e){

    }
}

希望这有帮助。