gpt4 book ai didi

java - 处理 onClick 事件时 getView 出现空指针异常

转载 作者:行者123 更新时间:2023-12-01 12:58:21 24 4
gpt4 key购买 nike

    @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 项目,这使得它可见,当我触摸删除时它崩溃,这里是调用堆栈(错误来自行 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

最佳答案

您正在将 ViewHolder 设置为 View 的标记。但是当您调用 getTag 时,它会被类型转换为 int,这就是问题所在。它不能是 int,因此异常(exception)。

关于java - 处理 onClick 事件时 getView 出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23714661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com