我知道有很多关于内存泄漏和方向变化的问题。我花了几个小时寻找和测试试图找出究竟发生了什么。我已将我的应用程序与活动和对话框片段配对。对话框片段在方向更改时消失。我知道并知道如何处理它。我只想尝试最少量的代码。当我点击微调器以展开它,然后改变方向时,我得到内存泄漏。如果没有它扩展,对话框就会消失 - 现在这很好。
任何人都可以看到为什么我扩展的Spinner会在方向更改时出现内存泄漏?是我还是我必须解决的安卓漏洞?
这是我的应用代码:
测试Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.danlconsulting.testing"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application >
<activity
android:name=".MainActivity"
android:label="Main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.java
package com.danlconsulting.testing;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyDialogFragment myDialog = new MyDialogFragment();
myDialog.show(getSupportFragmentManager(), "dlg");
}
}
MyDialogFragment.java
package com.danlconsulting.testing;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyDialogFragment extends DialogFragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_fragment,container,false);
}
}
dialog_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="1000dp"
android:orientation="vertical" >
<Spinner
android:id="@+id/inputSpinner"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.07" />
</LinearLayout>
</ScrollView>
和activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
编辑: 当然,我刚看到另一个似乎相同的问题。
Spinner drop-down list and screen orientation change problem
但是,我认为添加android:configChanges =“keyboardHidden | orientation”不是理想的解决方案?
我的LOGCAT:
11-07 14:59:34.834:E / WindowManager(14250):活动com.danlconsulting.testing.MainActivity泄露了最初添加的窗口com.android.internal.policy.impl.PhoneWindow$DecorView@40531e60 11-07 14:59:34.834:E / WindowManager(14250):android.view.WindowLeaked:Activity com.danlconsulting.testing.MainActivity泄露了窗口com.android.internal.policy.impl.PhoneWindow$DecorView@40531e60最初添加在这里 11-07 14:59:34.834:E / WindowManager(14250):在android.view.ViewRoot。(ViewRoot.java:273) 11-07 14:59:34.834:E / WindowManager(14250):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:174) 11-07 14:59:34.834:E / WindowManager(14250):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117) 11-07 14:59:34.834:E / WindowManager(14250):在android.view.Window $ LocalWindowManager.addView(Window.java:424) 11-07 14:59:34.834:E / WindowManager(14250):在android.app.Dialog.show(Dialog.java:241) 11-07 14:59:34.834:E / WindowManager(14250):在android.app.AlertDialog $ Builder.show(AlertDialog.java:823) 11-07 14:59:34.834:E / WindowManager(14250):在android.widget.Spinner.performClick(Spinner.java:265) 11-07 14:59:34.834:E / WindowManager(14250):在android.view.View $ PerformClick.run(View.java:9299) 11-07 14:59:34.834:E / WindowManager(14250):在android.os.Handler.handleCallback(Handler.java:587) 11-07 14:59:34.834:E / WindowManager(14250):在android.os.Handler.dispatchMessage(Handler.java:92) 11-07 14:59:34.834:E / WindowManager(14250):在android.os.Looper.loop(Looper.java:130) 11-07 14:59:34.834:E / WindowManager(14250):在android.app.ActivityThread.main(ActivityThread.java:3691) 11-07 14:59:34.834:E / WindowManager(14250):at java.lang.reflect.Method.invokeNative(Native Method) 11-07 14:59:34.834:E / WindowManager(14250):at java.lang.reflect.Method.invoke(Method.java:507) 11-07 14:59:34.834:E / WindowManager(14250):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:912) 11-07 14:59:34.834:E / WindowManager(14250):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670) 11-07 14:59:34.834:E / WindowManager(14250):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
private MyDialogFragment myDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myDialog = new MyDialogFragment();
myDialog.show(getSupportFragmentManager(), "dlg");
}
@Override
public void onConfigurationChanged( Configuration newConfig ) {
super.onConfigurationChanged( newConfig );
if( !myDialog.isHidden() ){
myDialog.dismiss();
}
}