单击列表视图项时显示警报对话框时出错

Error in showing alert dialog while clicking on a list view item

单击列表视图项时显示警告对话框时出错。

代码如下。这是在 android 中完成的程序 sqlite.The 检索到的数据显示在列表视图中,但警告对话框无法正常工作以进行编辑和删除。

请帮我写代码......

import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

public class Display11 extends Activity {
    ListView listview;
    PopupWindow popup;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display1);

        listview=(ListView)findViewById(R.id.listView1);
        Intent in=getIntent();
        ArrayList<String> a=in.getStringArrayListExtra("Key");
        ArrayAdapter<String> dis=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,a);
        listview.setAdapter(dis);
        listview.setTextFilterEnabled(true);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,long rowId) {
                // TODO Auto-generated method stub

        ///public void OnItemClick(AdapterView<?> parent, View view, int position,long rowId){
                int itemposition = position;
                String itemValue = (String)listview.getItemAtPosition(position); 
                // TODO Auto-generated method stub



                AlertDialog.Builder alert  = new AlertDialog.Builder(getApplicationContext());
                /*LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
                View view1 =inflater.inflate(R.layout.alert_dialog,null);
                alert.setView(view1);*/
                //ad.setView(inflater.inflate(R.layout.alert_dialog, null));
                popup = new PopupWindow(getBaseContext());
                //popup.setContentView(alert_)
                alert.setMessage("Database Manager");

                alert.setIcon(R.drawable.ic_launcher);
                alert.setTitle("Terms of Service");

                alert.setPositiveButton("edit",new DialogInterface.OnClickListener(){

                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        Intent positiveActivity=new Intent(getApplicationContext(),com.cerebtec.contactdb.PositiveActivity.class);
                    startActivity(positiveActivity);
                    }});

                alert.setNegativeButton("delete",new DialogInterface.OnClickListener(){

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Intent negativeActivity=new Intent(getApplicationContext(),com.cerebtec.contactdb.NegativeActivity.class);
                    startActivity(negativeActivity);
                    }});
                //ad.setNeutralButton("Cancel",null);
                //ad.setCancelable(false);


                //AlertDialog alert=ad.create();
                //alert.setView(view1);
                alert.create();
                alert.show();
            }});
        }}

您的问题出在这段代码中:

AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());

应用程序是 UI 内容的错误上下文,您需要 activity。你需要使用这个:

AlertDialog.Builder alert = new AlertDialog.Builder(Display11.this);