警报对话框 - 出错

时间:2013-04-09 11:17:49

标签: android android-alertdialog

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

我收到此错误

android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌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;
}

Plz帮助我在这个班级中显示警告对话框

3 个答案:

答案 0 :(得分:3)

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

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

使用 v.getRootView()。getContext()代替context或v.getContext()

答案 1 :(得分:1)

试试这个

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( DeteteAdapter.this);

答案 2 :(得分:0)

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

更改此行;

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                (Activity)context);

也许它可以帮到你