代码没有工作警报对话框

时间:2014-04-13 20:14:43

标签: android dialog

我只是想显示一个Android对话框,但我的活动每次都会崩溃。 我想显示一个包含2个EditText字段和2个按钮的对话框。

我正在尝试这样做,我做错了什么或可能有最简单的方法吗?

public class OsmRoadTaskActivity extends Activity {

Button Chercher, Annuler;
EditText startpoint, endpoint;
Context ctx;

@SuppressLint("CutPasteId")
@SuppressWarnings("unused")
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    ctx = this.getApplicationContext();
    setContentView(R.layout.activity_main);
    Chercher = (Button) findViewById(R.id.chercher);
    Annuler = (Button) findViewById(R.id.annuler);

    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    final AlertDialog alertd = alert.create();
    alert.setTitle("Itineraire");
    LayoutInflater inflater = this.getLayoutInflater();
    View layout = inflater.inflate(R.layout.activity_main, null, false);

    /*
     * final EditText startpoint = new EditText(OsmRoadTaskActivity.this);
     * LinearLayout.LayoutParams lp = new
     * LinearLayout.LayoutParams(LinearLayout
     * .LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
     * startpoint.setLayoutParams(lp); alert.setView(startpoint);
     */
    // final EditText startpoint =
    // (EditText)layout.findViewById(R.id.endpoint);
    final EditText startpoint = new EditText(this);
    final EditText endpoint = (EditText) layout.findViewById(R.id.endpoint);
    alert.setView(endpoint);
    alert.setView(startpoint);
    alert.setView(layout);
    alert.setView(Annuler);
    alert.setView(Chercher);
    alert.setPositiveButton("Chercher",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    new OsmRoadTask(ctx, startpoint.getText(), endpoint
                            .getText()).execute();
                }
            });
    alert.setNegativeButton("Annuler",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                    alertd.dismiss();

                }
            });

    alert.show();

}
    }

1 个答案:

答案 0 :(得分:1)

初始化构建器后创建AlertDialog对象。

public class OsmRoadTaskActivity extends Activity {

Button Chercher, Annuler;
EditText startpoint, endpoint;
Context ctx;

@SuppressLint("CutPasteId")
@SuppressWarnings("unused")
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
ctx = this.getApplicationContext();
setContentView(R.layout.activity_main);
Chercher = (Button) findViewById(R.id.chercher);
Annuler = (Button) findViewById(R.id.annuler);

final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Itineraire");
LayoutInflater inflater = this.getLayoutInflater();
View layout = inflater.inflate(R.layout.activity_main, null, false);

/*
 * final EditText startpoint = new EditText(OsmRoadTaskActivity.this);
 * LinearLayout.LayoutParams lp = new
 * LinearLayout.LayoutParams(LinearLayout
 * .LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
 * startpoint.setLayoutParams(lp); alert.setView(startpoint);
 */
// final EditText startpoint =
// (EditText)layout.findViewById(R.id.endpoint);
final EditText startpoint = new EditText(this);
final EditText endpoint = (EditText) layout.findViewById(R.id.endpoint);
alert.setView(endpoint);
alert.setView(startpoint);
alert.setView(layout);
alert.setView(Annuler);
alert.setView(Chercher);
alert.setPositiveButton("Chercher",
        new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                new OsmRoadTask(ctx, startpoint.getText(), endpoint
                        .getText()).execute();
            }
        });
alert.setNegativeButton("Annuler",
        new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                alertd.dismiss();

            }
        });
final AlertDialog alertd = alert.create();
alertd.show();

}
    }