StickyGridHeaders带动画的NullPointerException

时间:2013-06-13 09:12:28

标签: android android-gridview

我正在使用StickyGridHeaders和适配器。一切正常,直到我尝试对网格中的项目使用Animation(我想删除项目时淡出)。

getView的这部分工作:

public View getView(int position, View convertView, final ViewGroup parent) {
    ViewHolder holder;


    if (convertView == null) {
        convertView = myInflater.inflate(R.layout.item_savedsearchlist,
                null);

        holder = new ViewHolder();
        holder.Tv1 = (TextView) convertView.findViewById(R.id.textView1);

        convertView.setTag(holder);
    }

    else {
        holder = (ViewHolder) convertView.getTag();

    }

    holder.Deletebtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            Animation anim = AnimationUtils.loadAnimation(ctx,
                    android.R.anim.slide_out_right);

            anim.setAnimationListener(new Animation.AnimationListener() {

                removeThisItem(); 
                notifiyDataSetChanged; 

        }
    });

    return convertView;
}

这不是:

public View getView(int position, View convertView, final ViewGroup parent) {
    ViewHolder holder;


    if (convertView == null) {
        convertView = myInflater.inflate(R.layout.item_savedsearchlist,
                null);

        holder = new ViewHolder();
        holder.Tv1 = (TextView) convertView.findViewById(R.id.textView1);

        convertView.setTag(holder);
    }

    else {
        holder = (ViewHolder) convertView.getTag();

    }

    final View rc = holder.Tv1;     // I tried this with different Views from the item layout

    holder.Deletebtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            Animation anim = AnimationUtils.loadAnimation(ctx,
                    android.R.anim.slide_out_right);

            anim.setAnimationListener(new Animation.AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub

                    removeThisItem(); 
                    notifyDataSetChanged(); 


                }
            });

            rc.startAnimation(anim);

        }
    });


    return convertView;
}

第二个版本导致此错误:

06-13 11:01:59.957: E/AndroidRuntime(1045): FATAL EXCEPTION: main
06-13 11:01:59.957: E/AndroidRuntime(1045): java.lang.NullPointerException
06-13 11:01:59.957: E/AndroidRuntime(1045):     at com.tonicartos.widget.stickygridheaders.StickyGridHeadersGridView.dispatchDraw(StickyGridHeadersGridView.java:774)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.View.draw(View.java:11180)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.widget.AbsListView.draw(AbsListView.java:3769)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.drawChild(ViewGroup.java:2895)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.View.draw(View.java:11180)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.support.v4.view.ViewPager.draw(ViewPager.java:1574)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.drawChild(ViewGroup.java:2895)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.View.draw(View.java:11180)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.widget.FrameLayout.draw(FrameLayout.java:450)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.drawChild(ViewGroup.java:2895)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.View.draw(View.java:11180)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.widget.FrameLayout.draw(FrameLayout.java:450)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2295)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewRootImpl.draw(ViewRootImpl.java:2231)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1837)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2649)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.os.Looper.loop(Looper.java:137)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at android.app.ActivityThread.main(ActivityThread.java:4507)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at java.lang.reflect.Method.invokeNative(Native Method)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at java.lang.reflect.Method.invoke(Method.java:511)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
06-13 11:01:59.957: E/AndroidRuntime(1045):     at dalvik.system.NativeStart.main(Native Method)

我真的不知道该项目的膨胀布局与GridHeader有什么关系。任何想法请求? :)

1 个答案:

答案 0 :(得分:0)

用此初始化ctx,之后你可以使用这个

DetailsAdapter(MainActivity.this)

public DetailsAdapter(MainActivity ctx) {

        mInflater = LayoutInflater.from(ctx.getBaseContext());
    }

    Animation anim = AnimationUtils.loadAnimation(ctx,
                        android.R.anim.slide_out_right);