Use Android click event of this

public  class the MainActivity the extends the Activity 
{ 
    the Button BN; 
    @Override 
    public  void the onCreate (the Bundle savedInstanceState) 
    { 
        Super .onCreate (savedInstanceState); 
        the setContentView (R.layout.main); 
        BN = (the Button) the findViewById (R.id.bn);
         / / example anonymous inner classes as an event listener 
        bn.setOnClickListener ( new new OnClickListener to () 
        { 
            // implement event processing method: when the button is clicked, a jump to the next interface 
            @Override
             public  void the onClick (View V) 
            { 
                //Preparation intent objects 
                the Intent intent = new new the Intent ( the this , NextActivity. Class );
                 // start the next screen 
                startActivity (intent); 
            } 
        }); 
    } 
}

The above code segment error code. The place is being given Intent intent = new Intent (this, NextActivity.class); this place. Why? Because anonymous inner class, if the outer class (MainActivity) does not implement a corresponding event listener interface (such as OnClickListener here), this is not just the use of. Because we are new anonymous inner class directly, which would put this as this is OnClickListener. So will the error.
The solution to this situation is to use MainActivity.this instead of the this !
NOTE: If the external type (the MainActivity) to achieve the corresponding event listener interfaces (such as where OnClickListener),

public class MainActivity extends Activity implements View.OnClickListener{
        .........    
}

So this is belong to the category of use Activity itself as an event listener class, then you can use this.

 

Guess you like

Origin www.cnblogs.com/yangjj08/p/11317484.html