为什么这显示错误

时间:2016-01-11 06:20:42

标签: android

 public void addItemsOnSpinner1() {
                    Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
                    spinner1.setOnItemSelectedListener(this);
                    List<String> list= new ArrayList<String>();
                    list.add("none,i am healthy");
                    list.add("Diabetes");
                    list.add("cancer");
                    list.add("HIV/AIDS");
                    list.add("Tuberculosis");
                    list.add("Coronary Artery");
                    list.add("respiratory disease");
                    ArrayAdapter dataAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,list);
                    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spinner1.setAdapter(dataAdapter);
                 };

                public void onItemSelected(AdapterView<?> parent, View view,
                                           int pos, long id) {

                }
                public void onNothingSelected(AdapterView<?> parent) {
                    // Another interface callback
                }

错误

1. Error:(93, 52) error: no suitable constructor found for ArrayAdapter(<anonymous OnClickListener>,int,List<String>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,List) is not applicable

2. Error:(93, 52) error: no suitable constructor found for ArrayAdapter(<anonymous OnClickListener>,int,List<String>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,List) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,List) is not applicable
(actual argument <anonymous OnClickListener> cannot be converted to Context by method invocation conversion)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,Object[]) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,Object[]) 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)

7 个答案:

答案 0 :(得分:1)

下面:

10 < x < 100

if (yes.isChecked()) { editText3.setText(age + 15); //<<< } else if (no.isChecked()) { editText3.setText(age - 10); //<< } 传递给int方法可能导致EditText.setText例外问题。

使用NotFoundException: String resource ID #0x1String.valueOf设置为EditText中的文本。

并删除以下行:

int

或在editText3.setText(Integer.toString(age)); 内移动。

答案 1 :(得分:0)

试试button.setOnClickListener(new View.OnClickListener() 而不是

button.setOnClickListener(
            new Button.OnClickListener()

答案 2 :(得分:0)

试试这个

editText3.setText(age+"", TextView.BufferType.EDITABLE);

答案 3 :(得分:0)

尝试以这种方式在edittext中设置文本:

<body>

答案 4 :(得分:0)

尝试更改此内容,

  button.setOnClickListener(new View.OnClickListener() {
         @Override
            public void onClick(View v) {
                //your code of declaration    
            }
  });

if (yes.isChecked()) {
        editText3.setText(""+(age + 15));
   } else if (no.isChecked()) {
        editText3.setText(""+(age - 10));


 editText3.setText(String.valueOf(age));

答案 5 :(得分:0)

onClick方法之外进行初始化:

 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);

 Button button = (Button) findViewById(R.id.button);

 button.setOnClickListener(
   new Button.OnClickListener() {
     public void onClick(View view) {
       int age;
       age = 0 ;

     if (yes.isChecked()) {
     String age =String.valueof(age)+15;
     editText3.setText(age);
   } else if (no.isChecked()) {
     String age = String.valueof(age)-10;
     editText3.setText(age);
   }
     }    
   });

答案 6 :(得分:0)

在点击侦听器之外实例化您的视图。

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);