2012-06-21 36 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