2013-09-21 114 views
1

你好,我得到的錯誤:錯誤setonclickListener未定義類型按鈕

the method setonclickListener(new onclickListener(){}) is undefined for the type button

這裏是我的代碼:

public class Sliding extends Activity { 
    /** Called when the activity is first created. */ 
    private LinearLayout MenuList; 
    private Button btnToggleMenuList; 
    private int screenWidth; 
    private boolean isExpanded; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_sliding); 
     MenuList = (LinearLayout) findViewById(R.id.linearLayout2); 
     btnToggleMenuList = (Button) findViewById(R.id.button1); 
     DisplayMetrics metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     screenWidth = metrics.widthPixels; 

     btnToggleMenuList.setonclickListener(new onclickListener() { 
      public void onclick(View v) { 
       if (isExpanded) { 
        isExpanded = false; 
        MenuList.startAnimation(new CollapseAnimation(MenuList, 0,(int)(screenWidth*0.7), 20)); 
       }else { 
        isExpanded = true; 
        MenuList.startAnimation(new ExpandAnimation(MenuList, 0,(int)(screenWidth*0.7), 20)); 
       } 
       } 
     }); 
    } 
} 

誰能告訴我爲什麼?

+0

進口android.view.View.OnClickListener;並使用setOnClickListener而不是setonclickListener –

回答

1

替換此

btnToggleMenuList.setonclickListener(new onclickListener() { 
     public void onclick(View v) { 

通過

btnToggleMenuList.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 

另外,還要確保你有下面的import語句

import android.view.View.OnClickListener; 
0

這樣寫:

btnToggleMenuList.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 

不是:

btnToggleMenuList.setonclickListener(new onclickListener() { 
    public void onclick(View v) { 
0

試試這個:

btnToggleMenuList.setonclickListener(new onclickListener() { 

     @Override 
     public void onclick(View v) {