Почему я получаю ожидаемое выражение fab.setOnClickListener (View? - PullRequest
0 голосов
/ 14 ноября 2018

Почему я не могу создать всплывающее диалоговое окно внутри моего fab.setOnClickListener(View в методе onCreate?

Это из-за синтаксиса?Я пропускаю выражения?Мой код устарел, так как мой инструктор - старый курс удеми?

      public class MainActivity extends AppCompatActivity {

        private AlertDialog.Builder dialogBuilder;
        private AlertDialog dialog;
        private EditText groceryItem;
        private EditText getGroceryItem;
        private EditText quantity;
        private Button saveButton;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            FloatingActionButton fab = findViewById(R.id.fab);
       // === error appears on the next line ===
            fab.setOnClickListener(View,  

            createPopupDialog());

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }


         private void createPopupDialog() {

            dialogBuilder = new AlertDialog.Builder(this);
            View view = getLayoutInflater().inflate(R.layout.popup, null);
            groceryItem = view.findViewById(R.id.groceryItem);
            quantity = view.findViewById(groceryQty);
            saveButton = view.findViewById(R.id.saveButton);

            dialogBuilder.setView(view);
            dialog = dialogBuilder.create();
            dialog.show();



        }


    }

Ответы [ 2 ]

0 голосов
/ 14 ноября 2018

Я думаю, вы хотели использовать лямбда-выражение.Если это так, то это правильный синтаксис:

fab.setOnClickListener((View v) -> {
    createPopupDialog();
});
0 голосов
/ 14 ноября 2018

Вы не передаете правильные аргументы в setOnClickListener().Создайте слушателя так:

fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        createPopupDialog();
    }
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...