无法解析符号“OnClickListener”

时间:2015-09-17 11:00:47

标签: android

以下代码显示无法解析符号“ÖnClickListener”的错误,期待“;”和方法onClick(android.view.view)从未使用过。 导入的类。 下面是我的代码,有助于找到异常。

import android.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.view.View.OnClickListener;
import .Variables;

    public class MainActivity extends ActionBarActivity implements onClickListener (Says Cannnot resolve symbol OnClickListener) {
      //Initialize variables
      Variables
      var;


      // Main Program starts here-------------------------------------------------
      @
      Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          //Initiate spinners and adapters here
          var.spin_hv = (Spinner) findViewById(R.id.spn_hv);
          var.adapt = ArrayAdapter.createFromResource(this, R.array.str_hv, android.R.layout.simple_spinner_item);
          var.adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          var.spin_hv.setAdapter(var.adapt);

          // Method called after Spinner is selected from the menu
          var.spin_hv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@
              Override
              public void onItemSelected(AdapterView <? > parent, View view, int position, long id) {

                if (var.dbug) {
                  Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
                }
                //Transfer the selection into a variable to be used in calculation
                var.hv_string = parent.getItemAtPosition(position).toString();

                //Check for which type of heating value unit of measurement is selected
                if (var.hv_string.equals("kCal/m3")) {
                  var.lhv_flag = 1;
                }
                if (var.hv_string.equals("btu/ft3")) {
                  var.lhv_flag = 2;
                } else
                  var.lhv_flag = 3;

              }

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

              }
            }_______________error ")" expected



Error here as ";" & "}" expected

            public void onClick(View v) {
              switch (v.getId()) {
                case (R.id.but_calc):
                  butCalculateClick(v);
                  break;
              }

            }


          }   'error class or interface expected'
}

2 个答案:

答案 0 :(得分:3)

该例外不是由OnClickListener引起的。 这是因为setOnItemSelectedListener()方法只是在方法定义的末尾添加)};

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

                if (var.dbug) {
                  Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
                }
                //Transfer the selection into a variable to be used in calculation
                var.hv_string = parent.getItemAtPosition(position).toString();

                //Check for which type of heating value unit of measurement is selected
                if (var.hv_string.equals("kCal/m3")) {
                  var.lhv_flag = 1;
                }
                if (var.hv_string.equals("btu/ft3")) {
                  var.lhv_flag = 2;
                } else
                  var.lhv_flag = 3;

              }

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

              }
            });

此代码是正确的。

答案 1 :(得分:2)

使用以下代码从spinner获取侦听器。

var.spin_hv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

        }

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

        }
    });

你对onClickListener表示赞同,但你应该使用OnClickListener,这是一个愚蠢的拼写错误..

不要忘记调用view.setOnClickListener(this);在你的onCreate。这里的视图类似于你想要添加lisenter的按钮,文本,imageView。