我担心从泄漏金丝雀回来的信息。它显示了在UI上声明的所有变量,如片段中的材料按钮、材料卡片视图、文本视图、图像视图等,都导致了内存泄漏。我不知道为什么会这样。
private Window mWindow;
private Toolbar mToolbar;
private FloatingActionButton btnBack, btnNext;
private Button btnStart;
private TextView tvSetUpNotifications, tvTitle, tvDescription;
private ImageView ivToolbar;
private CardView cvNotification;
private Bundle args = new Bundle();
private NavController mNavController;
private View view;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
((BaseApplication) getActivity().getApplication()).getAppComponent().inject(this);
}
public WorkoutCheckInIntro() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_workout_checkin_intro, container, false);
// configure the Window variable to enable the colour to be set
mWindow = getActivity().getWindow();
mWindow.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
mWindow.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
mWindow.setStatusBarColor(ContextCompat.getColor(getActivity(), R.color.calm));
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Instantiate the Navigation Controller.
mNavController = Navigation.findNavController(view);
// Configure the bottom navbar
BottomNavigationView navBar = getActivity().findViewById(R.id.bottom_navigation);
navBar.setVisibility(View.VISIBLE);
// Method calls
assignVariables();
setOnClickListeners();
}
/**
* Method which assigns variables to elements in the XML file.
*/
private void assignVariables() {
// Initialising variables from the xml file.
btnStart = view.findViewById(R.id.checkInDailyButtonStart);
tvSetUpNotifications = view.findViewById(R.id.setUpNotificationsTextView);
cvNotification = view.findViewById(R.id.notification_cardView2);
// Configure the top toolbar
mToolbar = view.findViewById(R.id.toolbar_check_in_intro);
btnBack = mToolbar.findViewById(R.id.toolbar_back_button);
btnNext = mToolbar.findViewById(R.id.toolbar_next_button);
btnNext.hide();
tvTitle = mToolbar.findViewById(R.id.toolbar_workout_title);
tvTitle.setVisibility(View.VISIBLE);
tvTitle.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
tvTitle.setText(getString(R.string.check_in));
tvDescription = mToolbar.findViewById(R.id.toolbar_workout_description);
tvDescription.setVisibility(View.GONE);
tvDescription.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
ivToolbar = mToolbar.findViewById(R.id.toolbar_workout_background);
ivToolbar.setVisibility(View.VISIBLE);
ivToolbar.setImageResource(R.drawable.check_in_intro);
}
/**
* Method which sets onClickListeners to buttons
*/
private void setOnClickListeners() {
btnStart.setOnClickListener(this);
btnBack.setOnClickListener(this);
tvSetUpNotifications.setOnClickListener(this);
cvNotification.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.toolbar_back_button:
if (mNavController.getCurrentDestination().getId() == R.id.workoutCheckInIntro) {
mNavController.navigate(R.id.action_workoutCheckInIntro_to_workoutFragment);
}
break;
case R.id.checkInDailyButtonStart:
// Boolean used in the WorkoutCheckInDailyMood.Class so when the user clicks
// the back button it will return them to this activity.
args.putBoolean(WORKOUT_CHECKIN_DAILY_MOOD, true);
if (mNavController.getCurrentDestination().getId() == R.id.workoutCheckInIntro) {
mNavController.navigate(R.id.action_workoutCheckInIntro_to_workoutMood, args);
}
break;
case R.id.notification_cardView2:
// Boolean used in the NotificationsSetUp.Class so when the user clicks
// the back button it will return them to this activity.
args.putBoolean(RETURN_TO_CHECKIN_WORKOUT, true);
if (mNavController.getCurrentDestination().getId() == R.id.workoutCheckInIntro) {
mNavController.navigate(R.id.action_workoutCheckInIntro_to_notificationsSetUp, args);
}
break;
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
mToolbar = null;
view = null;
btnBack = null;
btnNext = null;
tvDescription = null;
tvTitle = null;
ivToolbar = null;
btnStart = null;
cvNotification = null;
tvSetUpNotifications = null;
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:fillViewport="true"
tools:context=".ui.workouts.checkin.WorkoutCheckInIntro">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<include
android:id="@+id/toolbar_check_in_intro"
layout="@layout/toolbar_workout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="20dp"
android:text="Workout Length"
android:textSize="@dimen/text_size_heading_16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar_check_in_intro" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="20dp"
android:text="3 MIN - 5 MIN"
android:textSize="@dimen/text_size_timer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="20dp"
android:lineSpacingExtra="5sp"
android:text="We recommend to do this workout daily to develop a habit to check in with yourself each day. This is helpful as many of us are so busy that we become disconnected from our own thoughts and emotions."
android:textSize="@dimen/text_size_normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<com.google.android.material.button.MaterialButton
android:id="@+id/checkInDailyButtonStart"
style="@style/btnStyleRed"
android:layout_width="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="Start"
app:icon="@drawable/ic_dumb_bell_white_16dp"
app:iconGravity="textStart"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/notification_cardView2"
app:layout_constraintVertical_bias="1.0"
/>
<com.google.android.material.card.MaterialCardView
android:id="@+id/notification_cardView2"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:clickable="true"
app:cardCornerRadius="10dp"
app:cardElevation="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/setUpNotificationsTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:gravity="center"
android:text="Set up daily notifications to remind you to check-in"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/text_size_normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/appCompatImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:adjustViewBounds="true"
app:srcCompat="@drawable/brain_insight"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/setUpNotificationsTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
───
│ GC Root: System class
│
├─ android.provider.FontsContract class
│ Leaking: NO (BaseApplication↓ is not leaking and a class is never leaking)
│ ↓ static FontsContract.sContext
├─ com.example.BaseApplication instance
│ Leaking: NO (WorkoutFragment↓ is not leaking and Application is a singleton)
│ ↓ BaseApplication.appComponent
├─ com.example.di.DaggerAppComponent instance
│ Leaking: NO (WorkoutFragment↓ is not leaking)
│ ↓ DaggerAppComponent.provideWorkoutAdapterProvider
├─ dagger.internal.DoubleCheck instance
│ Leaking: NO (WorkoutFragment↓ is not leaking)
│ ↓ DoubleCheck.instance
├─ com.example.adapters.RvAdapterWorkout instance
│ Leaking: NO (WorkoutFragment↓ is not leaking)
│ ↓ RvAdapterWorkout.mOnWorkoutListener
├─ com.example.ui.workouts.WorkoutFragment instance
│ Leaking: NO (WorkoutCheckInIntro↓ is not leaking and Fragment#mFragmentManager is not null)
│ ↓ WorkoutFragment.mFragmentManager
├─ androidx.fragment.app.FragmentManagerImpl instance
│ Leaking: NO (WorkoutCheckInIntro↓ is not leaking)
│ ↓ FragmentManagerImpl.mFragmentStore
├─ androidx.fragment.app.FragmentStore instance
│ Leaking: NO (WorkoutCheckInIntro↓ is not leaking)
│ ↓ FragmentStore.mActive
├─ java.util.HashMap instance
│ Leaking: NO (WorkoutCheckInIntro↓ is not leaking)
│ ↓ HashMap.table
├─ java.util.HashMap$Node[] array
│ Leaking: NO (WorkoutCheckInIntro↓ is not leaking)
│ ↓ HashMap$Node[].[3]
├─ java.util.HashMap$Node instance
│ Leaking: NO (WorkoutCheckInIntro↓ is not leaking)
│ ↓ HashMap$Node.value
├─ androidx.fragment.app.FragmentStateManager instance
│ Leaking: NO (WorkoutCheckInIntro↓ is not leaking)
│ ↓ FragmentStateManager.mFragment
├─ com.example.ui.workouts.checkin.WorkoutCheckInIntro instance
│ Leaking: NO (Fragment#mFragmentManager is not null)
│ ↓ WorkoutCheckInIntro.view
│ ~~~~
╰→ androidx.core.widget.NestedScrollView instance
Leaking: YES (ObjectWatcher was watching this because com.example.ui.workouts.checkin.WorkoutCheckInIntro received Fragment#onDestroyView() callback (references to its views should be cleared to prevent leaks))
key = 53dc4388-0db6-402a-9ee3-2db6617c98f5
watchDurationMillis = 192734
retainedDurationMillis = 187732
mContext instance of com.example.ui.main.MainActivity with mDestroyed = false
View#mParent is null
View#mAttachInfo is null (view detached)
View.mWindowAttachCount = 1
METADATA
Build.VERSION.SDK_INT: 30
Build.MANUFACTURER: Google
LeakCanary version: 2.4
App process name: com.
Analysis duration: 22667 ms
23 Leaks
┬───
│ GC Root: System class
│
├─ android.provider.FontsContract class
│ Leaking: NO (BaseApplication↓ is not leaking and a class is never leaking)
│ ↓ static FontsContract.sContext
├─ com.example.BaseApplication instance
│ Leaking: NO (Application is a singleton)
│ ↓ BaseApplication.appComponent
│ ~~~~~~~~~~~~
├─ com.example.di.DaggerAppComponent instance
│ Leaking: UNKNOWN
│ ↓ DaggerAppComponent.viewModelUsersProvider
│ ~~~~~~~~~~~~~~~~~~~~~~
├─ dagger.internal.DoubleCheck instance
│ Leaking: UNKNOWN
│ ↓ DoubleCheck.instance
│ ~~~~~~~~
╰→ com.example.persistence.viewmodel.ViewModelUsers instance
Leaking: YES (ObjectWatcher was watching this because com.example.persistence.viewmodel.ViewModelUsers received ViewModel#onCleared() callback)
key = 45b732d9-f6e0-4852-9b3c-8397c587f29f
watchDurationMillis = 223094
retainedDurationMillis = 218094
METADATA
Build.VERSION.SDK_INT: 30
Build.MANUFACTURER: Google
LeakCanary version: 2.4
App process name: com.
Analysis duration: 22667 ms
有一种比编写所有这些样板代码首先获取所有视图引用,然后将它们设置为null
更方便的方法
它被称为视图绑定。您还可以使用数据绑定,它允许您更容易地与视图的数据交互。
然而,它指出:
与FindViewByID
不同,视图绑定(以及数据绑定)确保空安全和类型安全。
在科特林,你还有一个选择。您可以使用Kotlin扩展(只是gradle文件中的另一个依赖项),并且可以直接通过视图的ID名称访问视图,而不需要findviewbyid
。此方法与视图/数据绑定方法进行了比较。我想再次指出,视图/数据绑定可以在Java和Kotlin中使用。这就是android试图使您的开发努力最小化的方式。尤其是在许多片段之间移动时(并且避免/减少所有片段中的锅炉板代码)!
我有一个创建Presenter实例的活动。在Presenter层中,我从存储库中获取一个可观察的实例。然后,我使用Subscriber的子类订阅Observable,然后将结果订阅对象添加到CompositeSubscription。因为调用订阅服务器的onNext()后,我需要修改活动,所以我还将Presenter的引用传递给订阅服务器。 现在,我想知道引用是如何工作的,什么时候才有资格进行垃圾
问题内容: 我认为我的android应用正在泄漏内存。我不是绝对确定这是问题所在。 应用程序打开时经常崩溃,并且logcat尝试加载位图图像时会显示“内存不足”异常。 崩溃后,我重新打开了该应用程序,它运行正常。Logcat会显示许多“ gc”,并且JIT表会不时地向上调整大小,而不会向下调整,直到应用程序因内存不足错误而崩溃。 这听起来像是内存泄漏吗?如果是这样,我该如何定位和关闭泄漏点。 这是
我正在Android应用程序中使用一些本机库,我想在某个时间点从内存中卸载它们。当装入本机库的类的类装入器被垃圾回收时,库被卸载。灵感:本土卸载。 如果ClassLoader用于加载某些类(可能导致内存泄漏),则不会收集垃圾。 本机库只能在应用程序中的一个ClassLoader中加载。如果仍然有旧的ClassLoader挂在内存中的某个地方,并且一个新的ClassLoader试图在某个时间点加载相
我正在使用libgdx制作一个实时壁纸应用程序,根据时间改变资产。对于e、 g从早上6点到下午6点,我有“早上图形”,之后我有“晚上图形”,从下午6点到早上6点。 我构建资产的方式如下 我有12个AtlasRegion类型的静态数组,1个静态纹理区域变量和1个静态纹理变量。 我有两个静态函数load晨()和loadEnight()用于加载资产。 在funcions中,我加载如下 对于所有数组,如果
本文向大家介绍Android 内存溢出和内存泄漏的问题,包括了Android 内存溢出和内存泄漏的问题的使用技巧和注意事项,需要的朋友参考一下 Android 内存溢出和内存泄漏的问题 在面试中,经常有面试官会问“你知道什么是内存溢出?什么是内存泄漏?怎么避免?”通过这篇文章,你可以回答出来了。 内存溢出 (OOM)是指程序在申请内存时,没有足够的内存空间供其使用,出现out of memory;
问题内容: 我正在阅读android docs http://developer.android.com/reference/com/google/android/gms/maps/MapFragment.html ,我碰到了这句话: 从GoogleMap获得的所有对象都与该视图相关联。重要的是不要在视图的生命周期之内保留对象(例如Marker)。否则将导致内存泄漏,因为无法释放视图。 我不完全了