简单的ViewPager应用程序导致内存泄漏错误

时间:2017-03-30 10:33:43

标签: android memory-leaks android-viewpager

我尝试制作一个简单的ViewPager应用程序示例,但我遇到了错误。我认为这是图像分辨率的错误,但在将ImageView更改为TextView并在其上编写示例文本后,我得到了同样的错误。 (在评论ViewPAger实现工作正常后)

  

channel'2d5df67 com.example.k.myapplication / com.example.k.myapplication.MainActivity(server)'〜频道无法恢复,将被处置!

public class MainActivity extends AppCompatActivity {
List<String> list;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


  list = new ArrayList<>();
    list.add("https://www.w3schools.com/css/trolltunga.jpg");
    list.add("http://www.esa.int/var/esa/storage/images/esa_multimedia/images/2016/03/ultraviolet_image_shows_the_sun_s_intricate_atmosphere/15891756-1-eng-GB/Ultraviolet_image_shows_the_Sun_s_intricate_atmosphere_node_full_image_2.jpg");
    list.add("http://pnge.org/wp-content/uploads/2017/03/1488980395_548_image.jpg");
    list.add("http://pnge.org/wp-content/uploads/2017/03/image.png");
    list.add("https://www.w3schools.com/css/trolltunga.jpg");
    list.add("http://www.esa.int/var/esa/storage/images/esa_multimedia/images/2016/03/ultraviolet_image_shows_the_sun_s_intricate_atmosphere/15891756-1-eng-GB/Ultraviolet_image_shows_the_Sun_s_intricate_atmosphere_node_full_image_2.jpg");
    list.add("http://pnge.org/wp-content/uploads/2017/03/1488980395_548_image.jpg");
    list.add("http://pnge.org/wp-content/uploads/2017/03/image.png");
    viewPager  = (ViewPager)findViewById(R.id.viewPAger);
    ViewPAgerAdapter adapter = new ViewPAgerAdapter(MainActivity.this,list);
    viewPager.setAdapter(adapter);

}}

适配器类

public class ViewPAgerAdapter extends PagerAdapter {

    private List<String> record;
    private Context context;
   private LayoutInflater inflater;
    public ViewPAgerAdapter(Context context, List<String>record) {
        this.record=record;
        this.context=context;
        inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        View view = inflater.inflate(R.layout.item,container);

        ImageView img = (ImageView)view.findViewById(R.id.image);
      Glide.with(context).load(record.get(position)).fitCenter().into(img);

        container.addView(view);
        return view;
    }

    @Override
    public int getCount() {
        return record.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }
}

项目布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/image"
    />
</LinearLayout>

活动布局

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.k.myapplication.MainActivity">


<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/viewPAger"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

尝试将此添加到您的清单 - android:configChanges =“keyboard | keyboardHidden | orientation | screenLayout | uiMode | screenSize | smallestScreenSize | mcc | mnc”