微调器给我一个错误,我无法解决它

时间:2016-01-14 13:41:35

标签: java android constructor

这是代码!当我运行它。它在logcat中给出错误。我在下面提供了logcat。请检查!帮助我解决此错误。

Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);

//error 1//
spinner1.setOnItemSelectedListener(this); 

List<String> list = new ArrayList<String>();
list.add("Automobile");
list.add("Business Services");
list.add("Computers");
list.add("Education");
list.add("Personal");
list.add("Travel");

//error2//
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); 

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);

**我收到的错误是我自己无法解决的。我需要有关这些错误的帮助**

错误

1.(85, 37) error: method setOnItemSelectedListener in class AdapterView<T> cannot be applied to given types;
required: OnItemSelectedListener
found: <anonymous OnClickListener>
reason: actual argument <anonymous OnClickListener> cannot be converted to OnItemSelectedListener by method invocation conversion
where T is a type-variable:
T extends Adapter declared in class AdapterView

2.Error:(95, 64) error: no suitable constructor found for ArrayAdapter(<anonymous OnClickListener>,int,List<String>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,List<String>) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,List<String>) is not applicable
(actual argument <anonymous OnClickListener> cannot be converted to Context by method invocation conversion)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,String[]) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable
(actual argument <anonymous OnClickListener> cannot be converted to Context by method invocation conversion)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(actual argument <anonymous OnClickListener> cannot be converted to Context by method invocation conversion)
constructor ArrayAdapter.ArrayAdapter(Context,int) is not applicable
(actual and formal argument lists differ in length)

这里是onClickListner的代码

button.setOnClickListener(
            new Button.OnClickListener() {

                public void onClick(View view) {

                    CheckBox yes = (CheckBox) findViewById(R.id.yes);
                    CheckBox no = (CheckBox) findViewById(R.id.no);
                    EditText ageText = (EditText) findViewById(R.id.ageText);
                    EditText editText = (EditText) findViewById(R.id.editText);
                    EditText editText3 = (EditText) findViewById(R.id.editText3);


                    String name = editText.getText().toString();


                    if (yes.isChecked()) {
                        int age = Integer.parseInt(ageText.getText().toString());
                        int c;
                        c = age - 7;
                        int z;
                        z = 2016 + c;

                        editText3.setText((name + " you might die in the year " + z) + "");

                    } else if (no.isChecked()) {
                        int age = Integer.parseInt(ageText.getText().toString());
                        int c;

                        c = age + 10;
                        int z;
                        z = 2016 + c;
                        editText3.setText((name + " you might die in the year " + z) + "");

2 个答案:

答案 0 :(得分:0)

它不像logcat中显示的那么平坦。

spinner1.setOnItemSelectedListener(this); 

应该是

spinner1.setOnItemSelectedListener(MyActivity.this); 

答案 1 :(得分:0)

如果您的Activity正在实现OnItemSelectedListener接口,那么您可以将上下文传递给spinner项目单击侦听器。

spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

否则,传递一个实现OnItemSelectedListener接口的匿名类实例。

if

错误2:

您可能正在尝试在匿名类中初始化适配器。将 [YourActivityName] .this 而不是 this 传递给ArrayAdapter的构造函数。

详细了解微调器Android - Spinner Tutorial