2011-12-04 56 views
2

我有以下代碼以前的工作。然後後,我開始對另一個活動的工作(實現ListActivity,並通過擴展ArrayAdapter提供每行自定義視圖),我開始獲取的RuntimeException:setOnClickListener抱怨說,我應該使用setOnItemClickListener

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ezport/com.ezport.EzportActivity}: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead 

這裏是代碼

package com.ezport; 

import android.app.Activity; 
import android.os.Bundle; 
import android.content.Intent; 
import android.view.View; 
import android.view.View.OnClickListener;  
public class EzportActivity extends Activity implements OnClickListener { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     View view_orders_button = findViewById(R.id.view_orders_button); 
     view_orders_button.setOnClickListener(this); 
     View submit_invoice_button = findViewById(R.id.submit_invoice_button); 
     submit_invoice_button.setOnClickListener(this); 
     View help_button = findViewById(R.id.help_button); 
     help_button.setOnClickListener(this); 
    } 

    public void onClick(View view) { 
     switch (view.getId()) { 
     case R.id.view_orders_button: 
      Intent order_intent = new Intent(this, OrderActivity.class); 
      startActivity(order_intent); 
      break; 
     case R.id.submit_invoice_button: 
      //Intent i = new Intent(this,) 
      break; 
     case R.id.help_button: 
      Intent help_intent = new Intent(this, HelpActivity.class); 
      startActivity(help_intent); 
      break; 
     } 
    } 
} 

什麼了我做錯了?

+0

的'ListView'這個活動? – havexz

+0

有沒有這方面的運氣?我有同樣的問題 – amadib

回答

1

試試這個:

Button button1 = (Button)findViewById(R.id.button); 
button1.setOnClickListener(this); 

這個工作對我來說,它可能解決您的問題,它看起來像好的代碼我想不明白爲什麼否則它不會是工作。

+0

我試過了,它確實有效。但它也可能是代碼的另一部分。無論如何,感謝您的幫助。 –

0

我剛剛有同樣的問題,但代碼編譯和工作正常之前。

實際發生的是,我已經在XML佈局文件中重新排序項目。之後(Eclicpe)菜單 - 項目 - 清潔我再次罰款。

0

您需要將其轉換爲特定的視圖。說,如果你在你添加使用的ImageButton則代替這個

View help_button = findViewById(R.id.help_button); 

使用此

ImageButton help_button = (ImageButton)findViewById(R.id.help_button);