简体   繁体   中英

Error in Adding onClickListeners to the buttons

I am writing a simple android program for app, in that there is 2 button , i am just trying click the button & display some msg by clicking , here is my code for two buttons .

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button buttonStart = (Button)findViewById(R.id.buttonStart);        
    buttonStart.setOnClickListener(startListener); // Register the onClick listener with             the implementation above

    Button buttonStop = (Button)findViewById(R.id.buttonStop);        
    buttonStop.setOnClickListener(stopListener); // Register the onClick listener with the implementation above
     }


   //Create an anonymous implementation of OnClickListener
    private OnClickListener startListener = new OnClickListener() {
     public void onClick(View v) {
      Log.d(logtag,"onClick() called - start button");              
      Toast.makeText(MainActivity.this, "The Start button was clicked.",          Toast.LENGTH_LONG).show();
      Log.d(logtag,"onClick() ended - start button");
    }
   };

    // Create an anonymous implementation of OnClickListener
    private OnClickListener stopListener = new OnClickListener() {
    public void onClick(View v) {
     Log.d(logtag,"onClick() called - stop button"); 
     Toast.makeText(MainActivity.this, "The Stop button was clicked.", Toast.LENGTH_LONG).show();
      Log.d(logtag,"onClick() ended - stop button");
    } 
    };

my error is saying that "OnClickListener cannot be resolved to a type"

can anyone help me in this . .

"OnClickListener cannot be resolved to a type"

Need to import View.OnclickListener in current Activity :

import android.view.View.OnclickListener;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM