如何激发不同的活动,点击按钮后在android系统? [英] How to fire different activities when buttons are clicked in android?

查看:210
本文介绍了如何激发不同的活动,点击按钮后在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发Android应用程序。我在我的应用程序4布局(1主要布局和3个子布局)。在我主要布局我用三个imagebuttons,如果被点击每个按钮启动的活动。即,一个按钮被按下时,转换到下一个布局。我用了onClicklistener()方法是单击按钮时处理该事件。问题是,当点击第一个按钮,当它成功地切换到下一个布局但点击另外两个按钮时,应用程序强制关闭。在每个子布局我用一个ListView显示一些内容。这里是code为在MainActivity:

I am currently developing an android app. I have 4 layouts in my app(1 main layout and 3 sub layouts). In my main layout I am using three imagebuttons and if each button is clicked it starts an activity. i.e., when a button is clicked it transitions over to the next layout. I used the onClicklistener() method to handle the event when the button is clicked. The problem is that when the first button is clicked it changes to the next layout successfully but when the other two buttons are clicked the app force closes. In each sub layout I use a listview to display some content. Here is the code for the mainactivity:

public class MainactivityActivity extends Activity implements View.OnClickListener  {
 @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainactivity);  
 ImageButton m1 = (ImageButton) findViewById(R.id.imageButton1);
 ImageButton m2 = (ImageButton) findViewById(R.id.imageButton2);   
 ImageButton m3 = (ImageButton) findViewById(R.id.imageButton3);
m1.setOnClickListener(this);
m2.setOnClickListener(this);
m3.setOnClickListener(this);
}


@Override
public void onClick(View v) {

           switch(v.getId()) {
               case R.id.imageButton1:
                    Intent intent= new Intent(MainactivityActivity.this,Inspire.class);
                    startActivity(intent);
                    break;
               case R.id.imageButton2:
                   Intent inte = new Intent(MainactivityActivity.this,Love.class);
                   startActivity(inte);
                   break;
               case R.id.imageButton3:
                   Intent inten = new Intent(MainactivityActivity.this,Other.class);
                   startActivity(inten);
                   break;
                   default:
       }
}}

在XML布局为每个按钮的onclick =onclick事件:

我也附加值机器人。当第一个图像按钮将其转移到下一个布局,但是当其他的图像按钮被点击我的应用程序强制关闭和我得到的错误。我试着谷歌上搜索,但我无法找到一个完美的解决方案。请帮我解决这个问题。在此先感谢

I also added the value android:onclick="onClick" in the xml layout for each button. when the first image button it transfers to the next layout but when the other image buttons are clicked my app force closes and I get errors. I tried googling it but I couldn't find a perfect solution. Please help me solve this. Thanks in advance

推荐答案

删除行安卓的onclick =的onClick从XML。因为在你的活动中,你已经在使用onclicklistener。 看到这个

Remove the line android:onclick="onClick" from your xml. Because in your activity you are already using onclicklistener. See this

此外 Inspire.java Love.java &安培; Other.java 类也应在manifest.xml声明。 看到这个

Furthermore Inspire.java, Love.java & Other.java classes should also be declared in the manifest.xml. See this

这篇关于如何激发不同的活动,点击按钮后在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆