2012-06-21 39 views
0

在視圖之間切換時出現錯誤。我在xml的每個圖像按鈕上都有android:onClick="onClick"。下面是代碼使用ImageButton切換多個視圖

selfHelp = (ImageButton)findViewById(R.id.selfhelpButton); 
    services = (ImageButton)findViewById(R.id.services); 
    messages = (ImageButton)findViewById(R.id.mailButton); 
    about = (ImageButton)findViewById(R.id.aboutButton); 
    more = (ImageButton)findViewById(R.id.moreButton); 
    selfHelp.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) {    
       Intent myIntent = new Intent(view.getContext(), SelfHelp.class); 
       startActivity(myIntent); 
     } 

    }); 
    Services.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) {   
      Intent myIntent = new Intent(view.getContext(), Services.class); 
      startActivity(myIntent); 
     } 

    }); 

    messages.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) {    
      Intent myIntent = new Intent(view.getContext(), Messages.class); 
      startActivity(myIntent); 
     } 

    }); 

    about.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) {    
      Intent myIntent = new Intent(view.getContext(), About.class); 
      startActivity(myIntent); 
     } 

    }); 

    more.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) {    
      Intent myIntent = new Intent(view.getContext(), More.class); 
      startActivity(myIntent); 
     } 

    }); 

我有另一頁上這個工作只有一個按鈕,但圖像按鈕不起作用。任何幫助將是巨大的

+0

請發表您的錯誤和/或堆棧跟蹤 – styler1972

回答

1

你有兩個選擇

  1. 每個ImageButton的去除android:onClick="onClick",因爲你 已經在重寫 默認的onClick()方法設置點擊收聽你的按鈕。
  2. 刪除所有setOnclickListener()方法和您的ImageButtons添加屬性:

    android:onClick="onClick" 
    android:clickable="true" 
    

在活動中使用:

public void onClick(View v){ 
Intent i = null; 
switch(v.getId()){ 
case R.id.services: 
     i = new Intent(this,Services.class); 
     startActivity(i); 
     break; 
case R.id.mailButton: 
.......... 
break; 

} 
+0

@BTT你可以發佈你的logcat嗎? –

+0

正如我發佈logcat我發現錯誤。 Android清單有其他文件的拼寫錯誤。我手工輸入了它。謝謝您的幫助 – BigT