Android 全局变量值未更改

Android 全局变量值未更改,android,android-alertdialog,Android,Android Alertdialog,我创建了一个按钮,单击该按钮可打开一个AlertDialog,用于输入位置为全局变量的输入。。问题是,即使在为变量位置分配了onClickListener外部的值之后,它仍然显示为null String location = ""; TextView details; TextView cityName; TextView temp; Button locationInput; static String tempString = ""; @Override protected void on

我创建了一个按钮,单击该按钮可打开一个AlertDialog,用于输入位置为全局变量的输入。。问题是,即使在为变量位置分配了onClickListener外部的值之后,它仍然显示为null

String location = "";
TextView details;
TextView cityName;
TextView temp;
Button locationInput;
static String tempString = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    details = (TextView) findViewById(R.id.details_field);
    cityName = (TextView) findViewById(R.id.city_name);
    temp = (TextView) findViewById(R.id.current_temperature_field);
    locationInput = (Button) findViewById(R.id.button_location_input);


    locationInput.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
            LayoutInflater inflater = MainActivity.this.getLayoutInflater();
            View view = inflater.inflate(R.layout.dialog_box,null);
            alertDialog.setView(view);
            alertDialog.setTitle("Enter the location");

            final EditText locationInput = (EditText) view.findViewById(R.id.location_input);
            locationInput.setInputType(InputType.TYPE_CLASS_TEXT);

            alertDialog.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    location = locationInput.getText().toString();
                    Log.i("Location",location);
                    locationInput.setVisibility(View.GONE);
                    tempString = location;
                    dialog.dismiss();
                }
            });

            alertDialog.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                }
            });

            AlertDialog create = alertDialog.create();
            create.show();
        }
    });

请将代码从AlertDialog替换为Dialog,如下所示,它应该适合您

Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Title...");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

您是否已将“location”变量声明为String&它是否声明为全局变量?您是否已声明了它。。。如有疑问,请再次检查editText R.id.location_输入是否存在于相应的布局中。用ctrl+B键检查。有时微小的错误会导致空指针在那里扩展。。。我检查过了…我没弄明白。。。我想使用用户在API url的警报对话框中输入的位置变量来获取天气。。那么这将如何帮助。。。?