-2
クリックするとAlertDialogが開き、場所がグローバル変数になります。問題は、vClickable locationに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からコードを交換してくださいあなたは文字列として「場所」変数を宣言した&それがグローバルに宣言DOEことがありますか? –
よろしくお願いします。 –
もう一度、editText R.id.location_inputがそれぞれのレイアウトに存在するかどうかを確認してください。 ctrl + Bで確認してください。ときどき小さな間違いがnullpointer Expを作成する –