Java 问题:是否在布局(Android)中创建了一个按钮?

Java 问题:是否在布局(Android)中创建了一个按钮?,java,android,Java,Android,我尝试在一个布局中使用两个按钮: 注释按钮,该按钮在下面工作 计算器按钮(按钮10) 我在super.oncreate上遇到此错误: "The method onCreate(Bundle) is undefined for the type Object" main.java: public class IzzynActivity extends Activity{ /** Called when the activity is first created. */ p

我尝试在一个布局中使用两个按钮:

  • 注释按钮,该按钮在下面工作
  • 计算器按钮(按钮10)
我在super.oncreate上遇到此错误:

"The method onCreate(Bundle) is undefined for the type Object" 
main.java:

public class IzzynActivity extends Activity{

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button wg = (Button) findViewById(R.id.button1);
        wg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(IzzynActivity.this, notes.class);
                IzzynActivity.this.startActivity(myIntent);
            }

            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                Button wg = (Button) findViewById(R.id.button10);
                wg.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        Intent intent = new Intent(IzzynActivity.this, calculator.class);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
        });

            }


}
    }
}

我不知道你做了什么,但你把你的代码搞砸了。我的假设是,你一直在复制教程,而没有阅读实际发生的事情,因此没有真正理解你在做什么

下面是您的代码应该是什么样子(未经测试,我现在刚刚输入了这个,但这是jist)


我不知道你在那门课上到底做了什么,但是如果你想在版面中有两个按钮,那么就把它们放在版面中,然后在活动的
onCreate
方法中搜索它们:

主布局图


通读这篇文章,找出发生了什么,并与你的进行比较,看看你的错误是什么。老实说,这会帮助你学习。:)#这个按钮很好用!但这让我回到了android主屏幕。这是为什么?去掉这行
finish()
。这将关闭您正在进行的活动,并将您送回上一个活动(我假设这是您的android主屏幕)。然后,您需要确保调用
startActivity(intent)
您以前使用的
finish()
。这将开始你在上面几行中创建的活动。你们两个都很棒,谢谢!!我观察了两个答案,我学到了很多,谢谢!!!!每个活动只允许一个onCreate方法。下面的两个答案解决了你的问题。也许你应该读几本HelloAndroid教程来了解你在做什么。按钮可以工作。但是,它不会带我去calculator.class,而是带我去主屏幕…@user1248404
setResult
当您使用
startActivityForResult()
从另一个活动开始此活动,并在活动结束时预期结果时使用。如果您只想在单击按钮时启动
计算器
类,则只需使用第一次单击侦听器中使用的内容:
Intent myIntent=new Intent(IzzynActivity.this,calculator.class);Izzynacitivity.this.startActivity(我的意图)。关闭当前活动时使用方法
finish()
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // find the first button and set an on click listener
        Button wg = (Button) findViewById(R.id.button1);
        wg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(IzzynActivity.this, notes.class);
                IzzynActivity.this.startActivity(myIntent);
            }
        });

        // find the next button and set an on click listener
        Button otherButton = (Button)findViewById(R.id.button10);
        otherButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                Intent intent = new Intent(IzzynActivity.this, calculator.class);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
     <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />
      <Button
        android:id="@+id/button10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button10" />
</LinearLayout>
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button wg1 = (Button) findViewById(R.id.button1);
        wg1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(IzzynActivity.this, notes.class);
                IzzynActivity.this.startActivity(myIntent);
            }
        });
        Button wg10 = (Button) findViewById(R.id.button10);
                wg10.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        Intent intent = new Intent(IzzynActivity.this, calculator.class);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
        });