Android按钮不起作用;其他按钮虽然有效

时间:2012-01-30 21:07:35

标签: android eclipse button

我有一个包含两个按钮的应用程序,两个按钮都是为了将​​用户带到同一页面。 这是他们的.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);
        }



});}





});};};

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

3 个答案:

答案 0 :(得分: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);
    }
});
}
}

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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 应在您的清单文件中声明