gpt4 book ai didi

android - 警报对话框 - 出现错误

转载 作者:行者123 更新时间:2023-11-29 16:08:20 26 4
gpt4 key购买 nike

我想在使用 arrayAdapter 扩展的类中显示警报对话框。

我收到这个错误

android.view.WindowManager$BadTokenException: 无法添加窗口 -- token null 不适用于应用程序

这是我的代码,我试过的

public class DeteteAdapter extends ArrayAdapter<Gold> {
private int contexts = Intent.FLAG_ACTIVITY_NEW_TASK;
private Context context;
private List<Gold> subjects = new ArrayList<Gold>();
private TextView subject;
private TextView date_day;
private TextView time;
private ProgressDialog pDialog;
String name;
JSONParser jsonParser = new JSONParser();
// url to create new product
private static String url_create_product = "http://ayyappagold.com/ayyappa/test.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";

public static String month;
public static String year;

public DeteteAdapter(Context context, int textViewResourceId,
List<Gold> objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.subjects = objects;
}

@Override
public int getCount() {
return this.subjects.size();
}

@Override
public Gold getItem(int index) {
return this.subjects.get(index);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
// ROW INFLATION
Log.d("ExamAdapter", "Starting XML Row Inflation ... ");

LayoutInflater inflater = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_gold, parent, false);
Log.d("ExamAdapter", "Successfully completed XML Row Inflation!");
}

// Get item
Gold text = getItem(position);
subject = (TextView) row.findViewById(R.id.textView1);

time = (TextView) row.findViewById(R.id.textView2);

String content = text.content;

String times = text.time;

subject.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
name = getItem(position).id.toString();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);

// set title
alertDialogBuilder.setTitle("Delete");

// set dialog message
alertDialogBuilder
.setMessage("This item will be deleted")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, close
// current activity
new DeleteProduct().execute();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();
}
});





subject.setText(content.replace("*", "\n"));

return row;
}

请帮我在这个类中显示警告对话框

最佳答案

我找到了答案。我很高兴与您分享,

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
v.getRootView().getContext());

使用 v.getRootView().getContext() 代替上下文或 v.getContext()

关于android - 警报对话框 - 出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15900422/

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