使用ViewPager从Image url-崩溃

时间:2017-03-13 09:23:05

标签: android android-viewpager

我在编译时没有收到任何错误,但是当我点击gridview项目时,应用程序就会被压缩,它将带我进行寻呼活动。

以下是Viewpager活动的完整代码:

window.unload

然后是适配器类:

    package com.example.samer.applicationformatech.activities;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.example.samer.applicationformatech.R;
import com.example.samer.applicationformatech.adapters.ImagePagerAdapter;

import java.util.ArrayList;

public class BirdsGalleryActivity extends AppCompatActivity {

    private String img0=getIntent().getExtras().getString("IMG0");
    private String img1=getIntent().getExtras().getString("IMG1");
    private String img2=getIntent().getExtras().getString("IMG2");
    private String img3=getIntent().getExtras().getString("IMG3");
    private String img4=getIntent().getExtras().getString("IMG4");



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


        final ArrayList<String> birds = new ArrayList<>();
        birds.add(new String(img0));
        birds.add(new String(img1));
        birds.add(new String(img2));
        birds.add(new String(img3));
        birds.add(new String(img4));

        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImagePagerAdapter adapter = new ImagePagerAdapter(this,birds);
        viewPager.setAdapter(adapter);
    }

}

XML:

package com.example.samer.applicationformatech.adapters;


import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.samer.applicationformatech.R;
import com.example.samer.applicationformatech.activities.BirdsGalleryActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class ImagePagerAdapter extends PagerAdapter {

    Context context;
    LayoutInflater layoutInflater;

    ArrayList<String> arrayList;

    public ImagePagerAdapter(Context context, ArrayList<String> arrayList) {
        this.context = context;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.arrayList = arrayList;
    }

    public ImagePagerAdapter(BirdsGalleryActivity birdsGalleryActivity) {
    }

    @Override
    public int getCount() {
        if(arrayList != null){
            return arrayList.size();
        }
        return 0;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = layoutInflater.inflate(R.layout.image_viewpager_layout, container, false);

        ImageView imageView = (ImageView) itemView.findViewById(R.id.viewPagerItem_image1);

        Picasso.with(context).load(arrayList.get(position));
               // .placeholder(R.drawable.image_uploading)
               // .error(R.drawable.image_not_found).into(imageView);

        container.addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.samer.applicationformatech.activities.BirdsGalleryActivity"
     >

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

</LinearLayout>

知道为什么吗?

1 个答案:

答案 0 :(得分:3)

您应该在imgN方法中初始化onCreate字符串,否则您过早地进行了操作,并且我认为您在NullPointerException处获得了Intent调用。

onCreate只能从Activity方法开始使用,现在您正在尝试在public class BirdsGalleryActivity extends AppCompatActivity { private String img0; private String img1; private String img2; private String img3; private String img4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pagerview); img0=getIntent().getExtras().getString("IMG0"); img1=getIntent().getExtras().getString("IMG1"); img2=getIntent().getExtras().getString("IMG2"); img3=getIntent().getExtras().getString("IMG3"); img4=getIntent().getExtras().getString("IMG4"); final ArrayList<String> birds = new ArrayList<>(); birds.add(new String(img0)); birds.add(new String(img1)); birds.add(new String(img2)); birds.add(new String(img3)); birds.add(new String(img4)); ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); ImagePagerAdapter adapter = new ImagePagerAdapter(this,birds); viewPager.setAdapter(adapter); } } 的构造函数运行时访问它。

new String()

一些进一步的建议:

  • 您可以在创建列表时忽略Bundle extras = getIntent().getExtras(); img0 = extras.getString("IMG0"); ... 次来电,无需出于明显原因复制字符串。

  • 你可以使用这个新代码获得额外的捆绑包,如下所示:

original_list=[['No', 'se', 'puede'], ['No', 'se', 'puede'], ['No', 'se', 'admiten']]
item_to_replace = ['No', 'se', 'admiten']

required_list = ['i' if x == item_to_replace else x for x in original_list]