0

我正在尝试传递我的书刊编号

UrlPagerAdapter pagerAdapter = new UrlPagerAdapter(this,items,issue);

然后在我的 UrlPagerAdapter 类中,我在某些方法中记录了问题值,以测试我的值是否已成功通过,是的,确实如此。但是当我尝试创建自己的方法来记录值​​时,它不起作用!

public int getIssueNumber(){
    Log.d("issue - getIssueNumber()", String.valueOf(issue));
    return issue;
}

这是我的 urlpageradapter 类

public class UrlPagerAdapter extends PagerAdapter {

    private List<String> mResources;
    private Context mContext;
    private int issue;

    public UrlPagerAdapter(Context context, List<String> resources, int issue){
        this.mResources = resources;
        this.mContext = context;
        this.issue = issue;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        ((GalleryViewPager)container).mCurrentView = ((UrlTouchImageView)object).getImageView();
    }


    @Override
    public Object instantiateItem(View collection, int position){
        UrlTouchImageView iv = new UrlTouchImageView(mContext);
        iv.setUrl(mResources.get(position));
        iv.setLayoutParams(new Gallery.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
        ((ViewPager) collection).addView(iv, 0);
        return iv;


    }

    @Override
    public void destroyItem(View collection, int position, Object view){
        ((ViewPager) collection).removeView((View) view);
    }

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

    public int getIssueNumber(){
        return issue;
    }

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

    @Override
    public void finishUpdate(View arg0){
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1){
    }

    @Override
    public Parcelable saveState(){
        return null;
    }

    @Override
    public void startUpdate(View arg0){
    }

}

在另一堂课中,我尝试测试该方法。我的应用程序崩溃了!

public class UrlTouchImageView extends RelativeLayout {
    protected ProgressBar mProgressBar;
    protected TouchImageView mImageView;

    protected Context mContext;
    UrlPagerAdapter pagerAdapter;
    String[] mImageIds;
    ArrayList<String>  mStringList= new ArrayList<String>();

    public UrlTouchImageView(Context ctx)
    {
        super(ctx);
        mContext = ctx;

        init();

    }
    public UrlTouchImageView(Context ctx, AttributeSet attrs)
    {
        super(ctx, attrs);
        mContext = ctx;
        init();
    }
    public TouchImageView getImageView() { return mImageView; }

    protected void init() {


        int v = pagerAdapter.getIssueNumber();
        Log.d(" v - ImageLoadTask", String.valueOf(v));

        mImageView = new TouchImageView(mContext);
        LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        mImageView.setLayoutParams(params);
        this.addView(mImageView);
        mImageView.setVisibility(GONE);

        mProgressBar = new ProgressBar(mContext, null, android.R.attr.progressBarStyleHorizontal);
        params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_VERTICAL);
        params.setMargins(30, 0, 30, 0);
        mProgressBar.setLayoutParams(params);
        mProgressBar.setIndeterminate(true);
        this.addView(mProgressBar);
    }

    public void setUrl(String imageUrl)
    {
        new ImageLoadTask().execute(imageUrl);
    }
    //No caching load
    public class ImageLoadTask extends AsyncTask<String, Integer, Bitmap>
    {
        //RETRIEVES LINK FROM GALLERYACTIVITY
        //READ MNT/SDCARD/....

        @Override
        protected Bitmap doInBackground(String... strings) {
            //String url = strings[0];

            Bitmap bm = null;
            try {
            File strPath = new File(Environment.getExternalStorageDirectory() + "/issue0");
            int lists = strPath.listFiles().length; 
            Log.d("number of items in array ",String.valueOf(lists));

            File yourDir = new File(strPath, "");
            for (File f : yourDir.listFiles()) {
                if (f.isFile())
                {
                    String name = f.getName();
                    String v = strPath + "/" + name;
                    mStringList.add(v);
                }
            }

            mImageIds = new String[mStringList.size()];
            mImageIds = mStringList.toArray(mImageIds);

                bm = BitmapFactory.decodeFile(mImageIds[0]);

            } catch (Exception e) {
                e.printStackTrace();
            }

            return bm;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            mImageView.setImageBitmap(bitmap);
            mImageView.setVisibility(VISIBLE);
            mProgressBar.setVisibility(GONE);
        }
    }
}

这是我的 logcat 输出

01-14 10:59:45.130: E/AndroidRuntime(21843): FATAL EXCEPTION: main
01-14 10:59:45.130: E/AndroidRuntime(21843): java.lang.NullPointerException
01-14 10:59:45.130: E/AndroidRuntime(21843):    at com.example.touchview.UrlTouchImageView.init(UrlTouchImageView.java:67)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at com.example.touchview.UrlTouchImageView.<init>(UrlTouchImageView.java:53)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at com.example.gallery.UrlPagerAdapter.instantiateItem(UrlPagerAdapter.java:58)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:801)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.support.v4.view.ViewPager.populate(ViewPager.java:930)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.view.View.measure(View.java:12723)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1017)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:555)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.view.View.measure(View.java:12723)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.view.View.measure(View.java:12723)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.view.View.measure(View.java:12723)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.view.View.measure(View.java:12723)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.os.Looper.loop(Looper.java:137)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at android.app.ActivityThread.main(ActivityThread.java:4446)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at java.lang.reflect.Method.invokeNative(Native Method)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at java.lang.reflect.Method.invoke(Method.java:511)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-14 10:59:45.130: E/AndroidRuntime(21843):    at dalvik.system.NativeStart.main(Native Method)

什么地方出了错?

4

1 回答 1

0

您的方法UrlTouchImageView.init()(在构造函数中调用)访问pagerAdapter此行中字段的值:

int v = pagerAdapter.getIssueNumber();

pagerAdapter从未设置过的值。我相信这是你 NPE 的原因。您必须将此字段设置为非null值。在构造函数或init()方法中。传入UrlPagerAdapter构造函数可以这样完成:

  1. UrlTouchImageView构造函数中传递值:

    public UrlTouchImageView(Context ctx, UrlPagerAdapter pagerAdapter) {
        super(ctx);
        mContext = ctx;
        this.pagerAdapter = pagerAdapter;
        init();
    }
    
  2. UrlPagerAdapter.instantiateItem()通过this:_

    UrlTouchImageView iv = new UrlTouchImageView(mContext, this);
    

这也是未记录您的问题编号的原因 - 因为在调用之前抛出了 NPE getIssueNumber(),所以Log.d()从不调用。

于 2013-01-14T03:06:40.143 回答