0
    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    ViewHolder holder;

    if (null == convertView) {
        view = inflater.inflate(R.layout.web_courses_list_item, null);
        // Set table view color for each new row
        view.setBackgroundColor(0xFFF0F0F0);

        // Create the view holder
        holder = new ViewHolder();
        holder.CourseName = (TextView) view.findViewById(R.id.course_name);
        holder.CourseDesc = (TextView) view.findViewById(R.id.course_desc);
        holder.DltButton = (Button) view.findViewById(R.id.btn_remove);
        final int pos = position;
        holder.DltButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(final View v) {
                if(v==null) {
                    Log.v("RemoveCheck", "view v is null");
                    return;
                }
                Builder builder = new Builder(mContext);
                builder.setTitle("Alert!!!!");
                builder.setMessage("Are you sure you want to delete "+mCoursesList.get(pos).getName());
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mCoursesList.get(pos).setVisible(false);
                        mCoursesList.remove(pos);
                        v.setVisibility(View.GONE);
                        CoursesChangeData(mCoursesList);
                    }
                });

                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

                builder.create().show();
            }
        });
        view.setTag(holder);
    } else {
        view = convertView;
        holder = (ViewHolder) view.getTag();
    }

    CourseData current = mCoursesList.get(position);
    holder.CourseName.setText(current.getName());
    holder.CourseDesc.setText(current.getDesc());
    if (mCoursesList.get(position).isVisible())
        holder.DltButton.setVisibility(View.VISIBLE);
    else
        holder.DltButton.setVisibility(View.GONE);
    return view;
}

Hey guys, I implemented this method of baseAdapter for handling clicking on the delete button. the delete button is invisible until a swipe on listView item, which makes it visible, when i touch on delete its crashing and here is the callstack (The error is from the line builder.create().show(); ; this is line 111):

05-17 21:56:15.579: E/AndroidRuntime(18584): FATAL EXCEPTION: main
05-17 21:56:15.579: E/AndroidRuntime(18584): Process: com.example.webcourse, PID: 18584
05-17 21:56:15.579: E/AndroidRuntime(18584): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
05-17 21:56:15.579: E/AndroidRuntime(18584):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at android.app.Dialog.show(Dialog.java:286)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at com.example.webcourse.CoursesListAdapter$1.onClick(CoursesListAdapter.java:111)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at android.view.View.performClick(View.java:4445)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at android.view.View$PerformClick.run(View.java:18429)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at android.os.Handler.handleCallback(Handler.java:733)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at android.os.Looper.loop(Looper.java:136)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at android.app.ActivityThread.main(ActivityThread.java:5081)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at java.lang.reflect.Method.invokeNative(Native Method)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at java.lang.reflect.Method.invoke(Method.java:515)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-17 21:56:15.579: E/AndroidRuntime(18584):    at dalvik.system.NativeStart.main(Native Method)
05-17 21:56:15.584: W/ActivityManager(2330):   Force finishing activity com.example.webcourse/.CoursesActivity
Blo
  • 11,903
  • 5
  • 45
  • 99

3 Answers3

1

you are setting ViewHolder as tag to the view. But when you are calling getTag, it is type casted to int, which is the issue. It cannot be int and so the exception.

essess
  • 277
  • 3
  • 7
0

You can't know if context is final or not. You should use: v.getContext()

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
0

You cant set view.gone, view is reused for other elements. After changing the data set, you need to invalidate the adapter. Because you remove the object from the list and dont invalidate, the adapter will try to access the last element, which causes the null pnt exception.

Check this thread Updating the list view when the adapter data changes

Community
  • 1
  • 1
Dror
  • 1