处理onClick事件时getView中的空指针异常

时间:2014-05-17 18:21:48

标签: java android listview android-listview android-adapter

    @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;
}

嘿伙计们,我实现了baseAdapter的这种方法来处理点击删除按钮。删除按钮是不可见的,直到在listView项上滑动,这使得它可见,当我触摸时删除它的崩溃,这里是callstack(错误来自行builder.create().show(); ;这是第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

3 个答案:

答案 0 :(得分:1)

您正在将ViewHolder设置为视图的标记。但是当你调用getTag时,它是类型转换为int,这是问题。它不能是int等异常。

答案 1 :(得分:0)

您无法知道上下文是否是最终的。 您应该使用:v.getContext()

答案 2 :(得分:0)

您无法设置view.gone,视图会重复用于其他元素。 更改数据集后,您需要使适配器无效。因为从列表中删除对象并且不会使其无效,适配器将尝试访问最后一个元素,这会导致null pnt异常。

检查此帖子Updating the list view when the adapter data changes