【问题标题】:Android Button Doesn't Work; Other Buttons Do Work ThoughAndroid 按钮不起作用;其他按钮确实有效
【发布时间】:2012-01-30 21:07:35
【问题描述】:

我有一个包含两个按钮的应用程序,这两个按钮都假定将用户带到同一页面。 这是他们的 .java 文件:

public class PageTwoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page2);

        Button home = (Button) findViewById(R.id.ButtonHome);
        home.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), GuideApplicationActivity.class);
            startActivityForResult(myIntent, 0);


    Button home2 = (Button) findViewById(R.id.ButtonHome2);
    home2.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            Intent myIntent2 = new Intent(view.getContext(), GuideApplicationActivity.class);
            startActivityForResult(myIntent2, 0);
        }



});}





});};};

有什么帮助吗?如果您需要查看任何其他文件,请询问。 提前致谢,

【问题讨论】:

  • 不起作用是什么意思?你有什么错误吗?
  • 如果它不起作用那么告诉我们你的意思是什么?你得到一些异常(如果是,那么添加 logCat)?还是什么都没有? ..etc,我们需要更多详细信息 :) 我们可以为您提供帮助,因为您的代码看起来不错,应该像魅力一样工作;)
  • 是的,你的代码和文件末尾的});};}; ??

标签: android eclipse button


【解决方案1】:

您的开始和结束标签有问题。试试这个:

public class MainMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page2);

    Button home = (Button) findViewById(R.id.ButtonHome);
    home.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), GuideApplicationActivity.class);
        startActivityForResult(myIntent, 0);
        }
        });

Button home2 = (Button) findViewById(R.id.ButtonHome2);
home2.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
        Intent myIntent2 = new Intent(view.getContext(), GuideApplicationActivity.class);
        startActivityForResult(myIntent2, 0);
    }
});
}
}

【讨论】:

  • 这行得通,谢谢! :) 另外,我很抱歉没有通过发布错误日志来明确我的帖子,我对开发场景还很陌生。我会在以后的帖子中记住这一点。
【解决方案2】:

尝试在您的 onClick(...) 方法中使用此代码。

        Intent myIntent2 = new Intent(PageTwoActivity.this, GuideApplicationActivity.class);
        PageTwoActivity.this.startActivityForResult(myIntent2, 0);

【讨论】:

    【解决方案3】:

    我认为您在代码中做错了什么。试试这个:

    import android.os.Bundle;
    import android.content.Intent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    
    public class PageTwoActivity extends Activity implements OnClickListener{
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.page2);
    
            Button home = (Button) findViewById(R.id.ButtonHome);
            home.setOnClickListener(this);
    
    
        Button home2 = (Button) findViewById(R.id.ButtonHome2);
        home2.setOnClickListener(this);
    
     }
    
    @Override
    public void onClick(View v){
    Intent myIntent  = new Intent(this, GuideApplicationActivity.class);
                startActivityForResult(myIntent, 0);
    }
    }
    

    注意:您的活动GuideApplicationActivity应该在您的清单文件中声明

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多