方法无法检索值

时间:2013-01-14 02:58:26

标签: android android-adapter

我正试图通过我的图书编号

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)

出了什么问题?

1 个答案:

答案 0 :(得分: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);
    
  3. 这也是问题编号未被记录的原因 - 因为在<{1}} getIssueNumber()的调用之前NPE被抛出,{{1}永远不会被召唤。